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 void merge(K[] S1, K[] S2, K[] S, Comparator comp) {

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 void mergeSort( K[] S, Comparator comp ){

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!