Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Explan the following code in words and how it implements merge sort, randomized

ID: 3673337 • Letter: E

Question

Explan the following code in words and how it implements merge sort, randomized quicksort, non-randomized quicksort, selection sort, and insertion sort.

15 public final class SortingLabClient {
16
17 /** Drives execution. */
18 public static void main(String[] args) {
19
20 // instantiate the SortingLab class
21 // using your group number as the key value
22 int key = 23;
23 SortingLab<String> sls = new SortingLab<String>(key);
24
25 // run each sort a few times before trying to
26 // collect timing data
27 String[] as = {"D", "B", "E", "C", "A"};
28 for (int i = 0; i < 10; i++) {
29 sls.sort1(as);
30 sls.sort2(as);
31 sls.sort3(as);
32 sls.sort4(as);
33 sls.sort5(as);
34 }
35
36 // generate timing data for one sort method using
37 // the "doubling strategy" from lecture and lab
38 SortingLab<Integer> sli = new SortingLab<Integer>(key);
39 int M = 2000000; // max capacity for array
40 int N = 10000; // initial size of array
41 double start;
42 double elapsedTime;
43 for (; N < M; N *= 2) {
44 Integer[] ai = getIntegerArray(N, Integer.MAX_VALUE);
45 start = System.nanoTime();
46 sli.sort3(ai);
47 elapsedTime = (System.nanoTime() - start) / 1000000000d;
48 System.out.print(N + " ");
49 System.out.printf("%4.3f ", elapsedTime);
50 }
51 }
52
53 /** return an array of random integer values. */
54 private static Integer[] getIntegerArray(int N, int max) {
55 Integer[] a = new Integer[N];
56 java.util.Random rng = new java.util.Random();
57 for (int i = 0; i < N; i++) {
58 a[i] = rng.nextInt(max);
59 }
60 return a;
61 }
62
63 }

Explanation / Answer

Hi, can you post SortingLab.java, so that i can explain all sorting.

public final class SortingLabClient {

/** Drives execution. */
public static void main(String[] args) {

// instantiate the SortingLab class
// using your group number as the key value
int key = 23;
SortingLab<String> sls = new SortingLab<String>(key);

// run each sort a few times before trying to
// collect timing data
String[] as = {"D", "B", "E", "C", "A"};
for (int i = 0; i < 10; i++) {
sls.sort1(as);
sls.sort2(as);
sls.sort3(as);
sls.sort4(as);
sls.sort5(as);
}

// generate timing data for one sort method using
// the "doubling strategy" from lecture and lab
SortingLab<Integer> sli = new SortingLab<Integer>(key);
int M = 2000000; // max capacity for array
int N = 10000; // initial size of array
double start;
double elapsedTime;
for (; N < M; N *= 2) {
Integer[] ai = getIntegerArray(N, Integer.MAX_VALUE);
start = System.nanoTime();
sli.sort3(ai);
elapsedTime = (System.nanoTime() - start) / 1000000000d;
System.out.print(N + " ");
System.out.printf("%4.3f ", elapsedTime);
}
}

/** return an array of random integer values. */
private static Integer[] getIntegerArray(int N, int max) {
Integer[] a = new Integer[N];
java.util.Random rng = new java.util.Random();
for (int i = 0; i < N; i++) {
a[i] = rng.nextInt(max);
}
return a;
}

}

/*

First we are creating SortingLab object that sort String array.
We have sort String array using all sorting technique(5 sorting).

Now, we have other SortingLab object that sort integer array,
Here, we start with an integerr array of size initially 10000, and sort using sort3 method.
We record time taken by sort3 method.
We keep doubling the size of integer array till size of array reaches at 2000000 and we sort each time
using sort3 and records time taken by sort3.

Here Integer array is randomly filled by integers, that id done by getIntegerArray() method
*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote