# Homework Solution: Perform the Insertion Sort and Merge Sort algorithm by editing/modifiying the below code and fi…

In Java: Perform the Insertion Sort and Merge Sort algorithm by editing/modifiying the below code and filling in the the missing code. In short discuss the time performance of both algorithms (compare). Please make sure the program works (I use Eclipse). Thank you. -------------------------------------------------------------------------------------------------------------------------------------------------------------- package sort; import java.util.*; public class Sort {   public static int[] insertion_sort (int[] array) { return array; /* * fill in your program */ }   public static int[] merge_sort (int[] array, int p, int r) { /* * fill in your program */ return array; }   public static int[] merge (int[] array, int p, int q, int r) { /* * fill in your program */ return array; }   /* * n: the size of the output array */ public static int[] generate_random_array (int n) { List<Integer> list; int[] array;   list = new ArrayList<Integer> (); for (int i = 1; i <= n; i++) list.add(new Integer(i));   Collections.shuffle(list, new Random(System.currentTimeMillis()));   array = new int[n]; for (int i = 0; i < n; i++) array[i] = list.get(i).intValue();   return array; }   /* * Input: an integer array * Output: true if the array is acsendingly sorted, otherwise return false */ public static boolean check_sorted (int[] array) { for (int i = 1; i < array.length; i++) { if (array[i-1] > array[i]) return false; } return true; }   public static void print_array (int[] array) { for (int i = 0; i < array.length; i++) System.out.print(array[i] + ", "); System.out.println(); }   public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Insertion sort starts ------------------"); for (int n = 10; n <= 100000; n=n*2) { int[] array = Sort.generate_random_array(n); long t1 = System.currentTimeMillis(); array = Sort.insertion_sort(array); long t2 = System.currentTimeMillis(); long t = t2 - t1; boolean flag = Sort.check_sorted(array); System.out.println(n + "," + t + "," + flag); } System.out.println("Insertion sort ends ------------------");   System.out.println("Merge sort starts ------------------"); for (int n = 10; n <= 100000; n=n*2) { int[] array = Sort.generate_random_array(n); long t1 = System.currentTimeMillis(); array = Sort.merge_sort(array, 1, n); long t2 = System.currentTimeMillis(); long t = t2 - t1; boolean flag = Sort.check_sorted(array); System.out.println(n + "," + t + "," + flag); } System.out.println("Merge sort ends ------------------"); } }

import java.util.*; public class Sort {

In Java:

Perform the Insertion Quality and Cohere Quality algorithm by editing/modifiying the beneath statute and satisfaction in the the waste statute. In narrow examine the age operation of twain algorithms (compare). Please fashion certain the program works (I reason Eclipse). Thank you.

————————————————————————————————————————————————————–

package quality;

import java.util.*;

public dispose Quality {

public static int[] insertion_quality (int[] vest) {

restore vest;

/*

*/

}

public static int[] cohere_quality (int[] vest, int p, int r) {

/*

*/

restore vest;

}

public static int[] cohere (int[] vest, int p, int q, int r) {

/*

*/

restore vest;

}

/*

* n: the bigness of the quenchedput vest

*/

public static int[] generate_random_vest (int n) {

List<Integer> register;

int[] vest;

register = strangelightlight VestList<Integer> ();

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

Collections.shuffle(list, strangelightlight Random(System.currentTimeMillis()));

vest = strangelightlight int[n];

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

array[i] = register.get(i).intValue();

restore vest;

}

/*

* Input: an integer vest

* Quenchedput: gentleman if the vest is acsendingly orderly, inadequately restore false

*/

public static boolean check_orderly (int[] vest) {

for (int i = 1; i < vest.length; i++) {

if (array[i-1] > vest[i])

restore false;

}

restore gentleman;

}

public static unoccupied imprint_vest (int[] vest) {

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

System.out.print(array[i] + “, “);

System.out.println();

}

public static unoccupied ocean(String[] args) {

// TODO Auto-generated system stub

System.out.println(“Insertion quality starts ——————“);

for (int n = 10; n <= 100000; n=n*2) {

int[] vest = Quality.generate_random_array(n);

long t1 = System.currentTimeMillis();

vest = Quality.insertion_sort(array);

long t2 = System.currentTimeMillis();

long t = t2 – t1;

boolean faint = Quality.check_sorted(array);

System.out.println(n + “,” + t + “,” + faint);

}

System.out.println(“Insertion quality ends ——————“);

System.out.println(“Cohere quality starts ——————“);

for (int n = 10; n <= 100000; n=n*2) {

int[] vest = Quality.generate_random_array(n);

long t1 = System.currentTimeMillis();

vest = Quality.merge_sort(array, 1, n);

long t2 = System.currentTimeMillis();

long t = t2 – t1;

boolean faint = Quality.check_sorted(array);

System.out.println(n + “,” + t + “,” + faint);

}

System.out.println(“Cohere quality ends ——————“);

}

}

## Expert Counter-argument

import java.util.*;

public dispose Quality {

public static int[] insertion_quality (int[] vest) {

int n = vest.length;
for (int j = 1; j < n; j++) {
int explanation = vest[j];
int i = j-1;
while ( (i > -1) && ( vest [i] > explanation ) ) {
vest [i+1] = vest [i];
i–;
}
array[i+1] = explanation;
}

restore vest;
}

public static int[] cohere_quality (int[] vest, int p, int r) {
/*
*/

int N = r – p;
if (N <= 1)
restore vest;
int mid = p + N/2;
// recursively cohere_sort
merge_sort(array, p, mid);
merge_sort(array, mid, r);
// cohere span orderly subarrays
int[] temp = strangelightlight int[N];
int i = p, j = mid;
for (int k = 0; k < N; k++)
{
if (i == mid)
temp[k] = vest[j++];
else if (j == r)
temp[k] = vest[i++];
else if (array[j]<array[i])
temp[k] = vest[j++];
else
temp[k] = vest[i++];
}
for (int k = 0; k < N; k++)
array[p + k] = temp[k];
restore vest;

}

/*
* n: the bigness of the quenchedput vest
*/
public static int[] generate_random_vest (int n) {
List<Integer> register;
int[] vest;

register = strangelightlight VestList<Integer> ();
for (int i = 1; i <= n; i++)

Collections.shuffle(list, strangelightlight Random(System.currentTimeMillis()));

vest = strangelightlight int[n];
for (int i = 0; i < n; i++)
array[i] = register.get(i).intValue();

restore vest;
}

/*
* Input: an integer vest
* Quenchedput: gentleman if the vest is acsendingly orderly, inadequately restore false
*/
public static boolean check_orderly (int[] vest) {
for (int i = 1; i < vest.length; i++) {
if (array[i-1] > vest[i])
restore false;
}
restore gentleman;
}

public static unoccupied imprint_vest (int[] vest) {
for (int i = 0; i < vest.length; i++)
System.out.print(array[i] + “, “);
System.out.println();
}

public static unoccupied ocean(String[] args) {
// TODO Auto-generated system stub
System.out.println(“Insertion quality starts ——————“);
for (int n = 10; n <= 100000; n=n*2) {
int[] vest = Quality.generate_random_array(n);
long t1 = System.currentTimeMillis();
vest = Quality.insertion_sort(array);
long t2 = System.currentTimeMillis();
long t = t2 – t1;
boolean faint = Quality.check_sorted(array);
System.out.println(n + “,” + t + “,” + faint);
}
System.out.println(“Insertion quality ends ——————“);

System.out.println(“Cohere quality starts ——————“);
for (int n = 10; n <= 100000; n=n*2) {
int[] vest = Quality.generate_random_array(n);
long t1 = System.currentTimeMillis();
vest = Quality.merge_sort(array, 1, n);
long t2 = System.currentTimeMillis();
long t = t2 – t1;
boolean faint = Quality.check_sorted(array);
System.out.println(n + “,” + t + “,” + faint);
}
System.out.println(“Cohere quality ends ——————“);
}

}

OUTPUT: