Write a program that obtains the execution time of selection sort, insertion sor
ID: 3689705 • Letter: W
Question
Write a program that obtains the execution time of selection sort, insertion sort, bubble sort, merge sort, quick sort, and radix sort. Your program should test all sort methods for input sizes of 10000, 20000, 30000, 40000, 50000, and 60000. The selection sort, bubble sort, and radix sort should also be tested for input sizes 100000 and 200000. Your program should create the data that is sorted from randomly generated integers and should output the results in a table.
(Hint: You can use the code template below to obtain the execution time.) long startTime = System.currentTimeMillis(); perform the task; long endTime = System.currentTimeMillis(); long executionTime = endTime - startTime;
Explanation / Answer
import java.io.IOException; class Test { public static void main(String[] args) throws IOException { int[] arr1 = getArray(10000); int[] arr2 = getArray(20000); int[] arr3 = getArray(30000); int[] arr4 = getArray(40000); int[] arr5 = getArray(50000); int[] arr6 = getArray(60000); int[] arr7 = getArray(100000); int[] arr8 = getArray(200000); System.out.println(" "); doTest(arr1); System.out.println(" "); doTest(arr2); System.out.println(" "); doTest(arr3); System.out.println(" "); doTest(arr4); System.out.println(" "); doTest(arr5); System.out.println(" "); doTest(arr6); System.out.println(" "); doTestMore(arr7); System.out.println(" "); doTestMore(arr8); } public static void doTest(int[] arr){ long startTime = System.currentTimeMillis(); selectionSort(arr); long endTime = System.currentTimeMillis(); long executionTime = endTime - startTime; System.out.println("SelectionSort("+arr.length+"):"+executionTime); startTime = System.currentTimeMillis(); insertionSort(arr); endTime = System.currentTimeMillis(); executionTime = endTime - startTime; System.out.println("insertionSort("+arr.length+"):"+executionTime); startTime = System.currentTimeMillis(); bubbleSort(arr); endTime = System.currentTimeMillis(); executionTime = endTime - startTime; System.out.println("bubbleSort("+arr.length+"):"+executionTime); startTime = System.currentTimeMillis(); (new MergeSort()).sort(arr); endTime = System.currentTimeMillis(); executionTime = endTime - startTime; System.out.println("MergeSort("+arr.length+"):"+executionTime); startTime = System.currentTimeMillis(); (new QuickSort()).sort(arr); endTime = System.currentTimeMillis(); executionTime = endTime - startTime; System.out.println("QuickSort("+arr.length+"):"+executionTime); startTime = System.currentTimeMillis(); (new RadixSort()).sort(arr); endTime = System.currentTimeMillis(); executionTime = endTime - startTime; System.out.println("RadixSort("+arr.length+"):"+executionTime); } public static void doTestMore(int[] arr){ long startTime = System.currentTimeMillis(); selectionSort(arr); long endTime = System.currentTimeMillis(); long executionTime = endTime - startTime; System.out.println("SelectionSort("+arr.length+"):"+executionTime); startTime = System.currentTimeMillis(); bubbleSort(arr); endTime = System.currentTimeMillis(); executionTime = endTime - startTime; System.out.println("bubbleSort("+arr.length+"):"+executionTime); startTime = System.currentTimeMillis(); (new RadixSort()).sort(arr); endTime = System.currentTimeMillis(); executionTime = endTime - startTime; System.out.println("RadixSort("+arr.length+"):"+executionTime); } public static int[] getArray(int n){ int[] arr = new int[n]; for(int i=0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.