I am implementing a merge sort algorithm. But, my code, for some reason, keeps copying one of the values and puts it back in to my array twice instead of doing a swap. I was wondering if someone can take a look and let me know where this might be happening and how I can fix it.
public static void mergeSort(int[] a, int p, int r) {
int q;
if (p < r) {
q = (p + r) / 2;
mergeSort(a, p, q);
mergeSort(a, q + 1, r);
merge(a, p, q, r);
}
}
private static void merge(int[] a, int p, int q, int r) {
int s1 = q - p + 1;
int s2 = r - q;
int B[] = new int[s1];
int C[] = new int[s2];
for (int i = 0; i < s1; i++) {
B[i] = a[p + i];
}
for (int j = 0; j < s2; j++) {
C[j] = a[q + 1 + j];
}
int i = 0;
int j = 0;
int k = p;
while (i < s1 && j < s2) {
if (B[i] <= C[j]) {
a[k] = B[i];
i++;
} else {
a[k] = C[j];
j++;
}
k++;
}
while (i < s1) {
a[k] = B[i];
i++;
k++;
}
while (j < s2) {
a[k] = B[j];
j++;
k++;
}
}
My current input for one instance is:
{ 1317884528, 359761860, -513283737, 369485540, 564749187 }
The output is:
{ -513283737, 359761860, 369485540, 369485540, 1317884528 }
I can tell that it is sorting somewhat properly, but it is having problems with swapping.