1. Engineering
  2. Computer Science
  3. please folow the instructions and do not add anything to...

Question: please folow the instructions and do not add anything to...

Question details

1 Problem Description Instructions. You are provided the skeleton code named Sort2.java. The source file is available on Canvas in a folder named HW2. Please modify the skeleton code to solve the following tasks . Task 1 (80 pts). Implement the Merge Sort algorithm as discussed in Lecture 2. (Hint: use the function checked sorted to check if your output is indeed sorted.) Task 2 (20 pts). Generate a report to discuss the time performance of the two algorithms. Compare it with their theoretical time complexity as discussed in the lecture. Plots and figures are encouraged to help draw the conclusion. See Figure 1 for an example of the plot 60 T 50 2 40 -←selection sort Insertion Sort (avg) Merge Sort n 30 5 E 20 10T 0 1,000 2,000 3,000 4,000 5,000 6,000 7,000 8,000 9,000 10,000 Problem Size (number of items to be sorted) Figure 1: An example of the time performance plot

Please folow the instructions and do not add anything to the code other than the code need it please

import java.util.*;

public class Sort2 {

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

* k: the maximum value in the array

*/

public static int[] generate_random_array (int n, int k) {

List<Integer> list;

int[] array;

Random rnd;

rnd = new Random(System.currentTimeMillis());

list = new ArrayList<Integer> ();

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

list.add(new Integer(rnd.nextInt(k+1)));

Collections.shuffle(list, rnd);

array = new int[n];

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

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

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("Merge sort starts ------------------

");

for (int n = 100000; n <= 1000000; n=n+100000) {

int[] array = Sort2.generate_random_array(n);

//Sort.print_array(array);

long t1 = System.currentTimeMillis();

array = Sort2.merge_sort(array, 0, n-1);

long t2 = System.currentTimeMillis();

long t = t2 - t1;

//Sort.print_array(array);

boolean flag = Sort2.check_sorted(array);

System.out.println(n + "," + t + "," + flag);

}

System.out.println("Merge sort ends ------------------");

}

}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution