## Question

## Transcribed Text

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

public class SortingComparison {public static void main(String[] args) {

int[] sizes = {10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000};

for (int size: sizes) {

System.out.println("SIZE = " + size + " elements");

int[] array = Utility.randomArray(size);

int[] arrayCopy;

int startTime, endTime, duration;

arrayCopy = Utility.copyArray(array);

startTime = (int) System.currentTimeMillis();

InsertionSort.sort(arrayCopy);

endTime = (int) System.currentTimeMillis();

duration = endTime - startTime;

System.out.println("Insertion Sort duration: " + duration + " ms");

arrayCopy = Utility.copyArray(array);

startTime = (int) System.currentTimeMillis();

MergeSort.sort(arrayCopy);

endTime = (int) System.currentTimeMillis();

duration = endTime - startTime;

System.out.println("Merge Sort duration: " + duration + " ms");

arrayCopy = Utility.copyArray(array);

startTime = (int) System.currentTimeMillis();

QuickSort.sort(arrayCopy);

endTime = (int) System.currentTimeMillis();

duration = endTime - startTime;

System.out.println("Quick Sort duration: " + duration + " ms");

arrayCopy = Utility.copyArray(array);

startTime = (int) System.currentTimeMillis();

HeapSort.sort(arrayCopy);

endTime = (int) System.currentTimeMillis();

duration = endTime - startTime;

System.out.println("Heap Sort duration: " + duration + " ms");

}

}

}

/*

* Utility class

*/

class Utility {

// Helper method to construct random array

static int[] randomArray(int size) {

int[] array = new int[size];

for (int i = 0; i < size; i++) {

array[i] = 1 + (int) (Math.random() * 1000000);

}

return array;

}

static int[] copyArray(int[] array) {

int[] copy = new int[array.length];

for (int i = 0; i < array.length; i++) {

copy[i] = array[i];

}

return copy;

}

}

/*

* Insertion Sort Implementation

*/

class InsertionSort {

public static void sort(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;

}

}

}

/*

* Merge Sort Implementation

*/

class MergeSort {

// Merges two subarrays of arr[].

// First subarray is arr[l..m]

// Second subarray is arr[m+1..r]

static void merge(int arr[], int l, int m, int r)

{

// Find sizes of two subarrays to be merged

int n1 = m - l + 1;

int n2 = r - m;

/* Create temp arrays */

int L[] = new int [n1];

int...

By purchasing this solution you'll be able to access the following files:

Solution.docx, Solution.xlsx and Solution.java.