Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program named SortingAlgorithm.java and implement the following sorting

ID: 3739555 • Letter: W

Question

 Write a program named SortingAlgorithm.java and implement the following sorting algorithm as we discussed in class:  public static void bubbleSort(int[] arr)  public static void selectionSort(int[] arr)  public static void insertionSort(int[] arr)  Download all data files and use each of these sorting algorithms to sort the data. Record the sorting time of each algorithm and tabulate results as follows:                bubble             selection          insertion   -----------------------------------------------------------  data1(m)       1                   2                  2  data2(n)     89956                589456             859   ...  ------------------------------------------------------------  (m) for millisecond (n) for nano time   To test all data files, you need to download your program and all data files, then test your sorting algorithm in local computer.  The first line in the data file represents two numbers N and M, followed by N line and each line contains M integers. Use those numbers to create an array and use each of the sorting method to test the time spend to sort the numbers.

Explanation / Answer

import java.io.*;

class SortingAlgorithm

{

public static void bubbleSort(int arr[])

{

int n = arr.length;

for (int i = 0; i < n-1; i++)

for (int j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1])

{

// swap temp and arr[i]

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

public static void selectionsort(int arr[])

{

int n = arr.length;

// One by one move boundary of unsorted subarray

for (int i = 0; i < n-1; i++)

{

// Find the minimum element in unsorted array

int min_idx = i;

for (int j = i+1; j < n; j++)

if (arr[j] < arr[min_idx])

min_idx = j;

// Swap the found minimum element with the first

// element

int temp = arr[min_idx];

arr[min_idx] = arr[i];

arr[i] = temp;

}

}

  

public static void insertionsort(int arr[])

{

int n = arr.length;

for (int i=1; i<n; ++i)

{

int key = arr[i];

int j = i-1;

/* Move elements of arr[0..i-1], that are

greater than key, to one position ahead

of their current position */

while (j>=0 && arr[j] > key)

{

arr[j+1] = arr[j];

j = j-1;

}

arr[j+1] = key;

}

}

/* Prints the array */

void printArray(int arr[])

{

int n = arr.length;

for (int i=0; i<n; ++i)

System.out.print(arr[i] + " ");

System.out.println();

}

// Driver method to test above

public static void main(String args[])

{

SortingAlgorithm ob1 = new SortingAlgorithm();

SortingAlgorithm ob2 = new SortingAlgorithm();

SortingAlgorithm ob3 = new SortingAlgorithm();

int arr[] = {64, 34, 25, 12, 22, 11, 90};

ob1.bubbleSort(arr);

ob2.selectionsort(arr);

ob3.insertionsort(arr);

System.out.println("Sorted array after bubble sort");

ob1.printArray(arr);

System.out.println("Sorted array after selection sort");

ob2.printArray(arr);

System.out.println("Sorted array after insertion sort");

ob3.printArray(arr);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote