Question: I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same

I need to program 3 and add to program 2 bellows:

Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array.

____________________>>>>>>>>>>>>>>>>>>>>___________________________

(This is program 2 code that I did : ) ---->>>>>> code bellow from program 2

java program - Algorithms

Write a program that randomly generates 100,000 integers into an array.

Write a method that sorts this large array using shell sort.

Write a method that sorts this large array using insertion sort.

Write a method that sorts this large array using selection.

Place code in the main method to evaluate the timing of the execution of each sort algorithm and report on each of them

Code program 2

SortDemo.java: import java.util.Random;

public class SortDemo {

public static int[] createRandomArray(int size) {

int array[] = new int[size];

Random r = new Random();

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

array[i] = r.nextInt() % (int) (size * 1.8);

}

return array;

}

public static int[] getArrayCopy(int[] arr) {

int newArr[] = new int[arr.length];

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

newArr[i] = arr[i];

}

return newArr;

}

public static void shellSort(int array[]) {

int gap = array.length / 2;

boolean swapflag;

while (gap > 0) {

swapflag = true;

while (swapflag) {

swapflag = false;

for (int s = 0; s < (array.length - gap); s++) {

if (array[s] > array[s + gap]) {

int temp = array[s];

array[s] = array[s + gap];

array[s + gap] = temp;

swapflag = true;

}

}

}

gap = gap / 2;

}

}

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;

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

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

j = j - 1;

}

arr[j + 1] = key;

}

}

public static void selectionSort(int arr[]) {

int n = arr.length;

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

int min_idx = i;

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

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

min_idx = j;

int temp = arr[min_idx];

arr[min_idx] = arr[i];

arr[i] = temp;

}

}

public static void main(String[] args) {

final int SIZE = 100000;

int arr1[] = createRandomArray(SIZE);

int arr2[] = getArrayCopy(arr1);

int arr3[] = getArrayCopy(arr1);

// Now all the 3 arrays have exactly same numbers

long start, end;

start = System.currentTimeMillis();

shellSort(arr1);

end = System.currentTimeMillis();

System.out.println("Shell sort time: " + (end - start) + " milliseconds.");

start = System.currentTimeMillis();

insertionSort(arr2);

end = System.currentTimeMillis();

System.out.println("Insertion sort time: " + (end - start) + " milliseconds.");

start = System.currentTimeMillis();

selectionSort(arr3);

end = System.currentTimeMillis();

System.out.println("Selection sort time: " + (end - start) + " milliseconds.");

}

}

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!