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 ------------------"); } }

    Expert Answer

     
    import java.util.*; public class Sort {

    In Java:

    Perform the Insertion Rank and Merge Rank algorithm by editing/modifiying the underneath legislation and satisfaction in the the detriment legislation. In concise examine the duration accomplishment of twain algorithms (compare). Please establish secure the program works (I correction Eclipse). Thank you.

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

    package rank;

    import java.util.*;

    public asrank Rank {

     

    public static int[] insertion_rank (int[] place) {

    restore place;

    /*

    * store in your program

    */

    }

     

    public static int[] merge_rank (int[] place, int p, int r) {

    /*

    * store in your program

    */

    restore place;

    }

     

    public static int[] merge (int[] place, int p, int q, int r) {

    /*

    * store in your program

    */

    restore place;

    }

     

    /*

    * n: the magnitude of the quenchedput place

    */

    public static int[] generate_random_place (int n) {

    List<Integer> roll;

    int[] place;

     

    roll = novel PlaceList<Integer> ();

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

    list.add(novel Integer(i));

     

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

     

    place = novel int[n];

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

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

     

    restore place;

    }

     

    /*

    * Input: an integer place

    * Quenchedput: penny if the place is acsendingly orderly, differently restore false

    */

    public static boolean check_orderly (int[] place) {

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

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

    restore false;

    }

    restore penny;

    }

     

    public static destitute imprint_place (int[] place) {

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

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

    System.out.println();

    }

     

    public static destitute ocean(String[] args) {

    // TODO Auto-generated course stub

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

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

    int[] place = Rank.generate_random_array(n);

    long t1 = System.currentTimeMillis();

    place = Rank.insertion_sort(array);

    long t2 = System.currentTimeMillis();

    long t = t2 – t1;

    boolean succumb = Rank.check_sorted(array);

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

    }

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

     

    System.out.println(“Merge rank starts ——————“);

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

    int[] place = Rank.generate_random_array(n);

    long t1 = System.currentTimeMillis();

    place = Rank.merge_sort(array, 1, n);

    long t2 = System.currentTimeMillis();

    long t = t2 – t1;

    boolean succumb = Rank.check_sorted(array);

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

    }

    System.out.println(“Merge rank ends ——————“);

    }

    }

    Expert Rejoinder

     

    import java.util.*;

    public asrank Rank {

    public static int[] insertion_rank (int[] place) {

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

    restore place;
    }

    public static int[] merge_rank (int[] place, int p, int r) {
    /*
    * store in your program
    */

    int N = r – p;
    if (N <= 1)
    restore place;
    int mid = p + N/2;
    // recursively merge_sort
    merge_sort(array, p, mid);
    merge_sort(array, mid, r);
    // merge couple orderly subarrays
    int[] temp = novel int[N];
    int i = p, j = mid;
    for (int k = 0; k < N; k++)
    {
    if (i == mid)
    temp[k] = place[j++];
    else if (j == r)
    temp[k] = place[i++];
    else if (array[j]<array[i])
    temp[k] = place[j++];
    else
    temp[k] = place[i++];
    }
    for (int k = 0; k < N; k++)
    array[p + k] = temp[k];
    restore place;

    }

    /*
    * n: the magnitude of the quenchedput place
    */
    public static int[] generate_random_place (int n) {
    List<Integer> roll;
    int[] place;

    roll = novel PlaceList<Integer> ();
    for (int i = 1; i <= n; i++)
    list.add(novel Integer(i));

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

    place = novel int[n];
    for (int i = 0; i < n; i++)
    array[i] = roll.get(i).intValue();

    restore place;
    }

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

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

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

    System.out.println(“Merge rank starts ——————“);
    for (int n = 10; n <= 100000; n=n*2) {
    int[] place = Rank.generate_random_array(n);
    long t1 = System.currentTimeMillis();
    place = Rank.merge_sort(array, 1, n);
    long t2 = System.currentTimeMillis();
    long t = t2 – t1;
    boolean succumb = Rank.check_sorted(array);
    System.out.println(n + “,” + t + “,” + succumb);
    }
    System.out.println(“Merge rank ends ——————“);
    }

    }

    OUTPUT: