我试图将修改后的合并排序编写为练习。但我被困在为什么我的代码不工作,我不知道问题在哪里?!任何暗示或帮助都是值得欣赏的。提前谢谢。
这是我的代码:
public class MergeSort {
public static void mergeSort(int[] list) {
mergeSort(list, 0, list.length - 1);
}
private static void mergeSort(int[] list, int low, int high) {
if (low < high) {
int middle = (high + low) / 2;
mergeSort(list, low, middle);
mergeSort(list, middle + 1, high);
int[] temp = merge(list, low, high);
System.arraycopy(temp, 0, list, low, high - low + 1);
}
}
private static int[] merge(int[] list, int low, int high) {
int low1 = low;
int high1 = high;
int mid = (low + high) / 2;
int end_low = mid;
int start_high = mid + 1;
while ((low <= end_low) && (start_high <= high1)) {
if (list[low] < list[start_high]) {
low++;
} else {
int temp = list[high - low + 1];
for (int k = start_high - 1; k >= low; k--) {
list[k + 1] = list[k];
}
list[low] = temp;
low++;
end_low++;
start_high++;
}
}
return list;
}
public static void main(String[] args) {
int[] list = { 2, 3, 2, 5, 6, 1, -2, 3, 14, 12, -5 };
mergeSort(list);
for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
}