Question: Algorithm analysis of Merge Sort and Quick Sort Java code need help keeping track of the wins for merge sort and quick sort program counts

Algorithm analysis of Merge Sort and Quick Sort

Java code

need help keeping track of the wins for merge sort and quick sort

program counts the time it takes merge sort and quicksort to sort an array of random integers a total of 20 times.

need to keep track of the times that merge sort is faster then quick sort or vice verse during the 20 times.

public class SortTester { public static void main(String[] args) { int arr[] = { 34, 67, 23, 19, 122, 300, 2, 5, 17, 18, 5, 4319, -40, 23}; //Display the sorted array using mergeSort Sort.mergeSort(arr); System.out.println("Merge Sort:"); System.out.println(Arrays.toString(arr)); //Display the sorted array using quickSort. Sort.quickSort(arr); System.out.println("Quick Sort:"); System.out.println(Arrays.toString(arr)); int array10[] = new int[10]; int array100[] = new int[100];

Random rand = new Random(); for(int i=0; i<10; ++i ){ array10[i] = rand.nextInt(1000000) + 1; }

for(int i=0; i<100; ++i ){ array100[i] = rand.nextInt(1000000) + 1; }

System.out.println("Array size : 10"); System.out.print("Merge sort : "); long startTime = System.nanoTime(); // starts time for(int i=1;i<21;i++) // loops 20 times filling the array with 10 random integers { Sort.mergeSort(array10); // Merge Sorts the array } //NEED HELP WITH KEEPING TRACK OF WHICH ONE IS FASTER DURING EACH LOOP AND ADDING TO A COUNTER long finish = System.nanoTime(); // stops the time long duration = finish - startTime; //calculates duraction System.out.println(duration); System.out.print("Quick sort : "); // does same as above only with quick sort long startTime1 = System.nanoTime(); for(int i=1;i<21;i++) //NEED HELP WITH KEEPING TRACK OF WHICH ONE IS FASTER DURING EACH LOOP AND ADDING TO A COUNTER { Sort.quickSort(array10); } long finish1 = System.nanoTime(); long duration1 = finish1 - startTime1; System.out.println(duration1); System.out.println("------------------------------------------");

//100 random #s System.out.println("Array size : 100"); System.out.print("Merge sort : "); long startTime2 = System.nanoTime(); for(int i=1;i<21;i++) { Sort.mergeSort(array100); } long finish2 = System.nanoTime(); long duration2 = finish2 - startTime2; System.out.println(duration2); System.out.print("Quick sort : "); long startTime3 = System.nanoTime(); for(int i=1;i<21;i++) { Sort.quickSort(array100); } long finish3 = System.nanoTime(); long duration3 = finish3 - startTime3; System.out.println(duration3); System.out.println("------------------------------------------");

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!