Question: NEED HELP IN JAVA: Ques. Review the following code. a. Clearly state if this code represents a stable sort or an unstable sort. b. If
NEED HELP IN JAVA:
Ques. Review the following code.
a. Clearly state if this code represents a stable sort or an unstable sort.
b. If it is a stable sort clearly explain why it is a stable sort. If it is an unstable sort clearly explain why it is an unstable sort and how to fix it so that it is a stable sort.
import java.util.Arrays;
import java.util.Comparator;
public class Sort {
public static
int i = 0, j = 0;
while (i + j < S.length) {
if (j == S2.length || (i < S1.length && comp.compare(S1[i], S2[j]) < 0)) {
S[i + j] = S1[i++];
} else {
S[i + j] = S2[j++];
}
}
}
public static
int n = S.length;
if ( n < 2 ) return;
int mid = n / 2;
K[] S1 = Arrays.copyOfRange(S, 0, mid);
K[] S2 = Arrays.copyOfRange(S, mid, n);
mergeSort(S1, comp);
mergeSort(S2, comp);
merge(S1, S2, S, comp);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
