【发布时间】:2021-03-29 14:12:19
【问题描述】:
我正在编写 3 种方法来实现递归合并排序,但参数数量有限(无 aux、lo、mid、hi)。我认为我所做的工作,但它没有返回排序数组,尽管它运行时没有任何编译错误。我已经摆弄了 4 个小时,似乎无法弄清楚我做错了什么,没有合并有序数组。我只从我的助教那里得到了非常模糊的输入,并且能够解决我遇到的一些问题,但是该方法仍然不能对项目数组进行排序。欢迎就我在这里可能做错的事情提出任何建议。谢谢!
public static void sort(Comparable[] a) {
//Sort a recursively
a = mergeSort(a);
}
/**
*
* Method recursively splits array into halves until length becomes 1, then
* Calls merge to merge the split arrays back together.
* @param a Comparable[]
* @return a Comparable[]
*/
public static Comparable[] mergeSort(Comparable[] a) {
if (a.length <= 1) {
return a;
} else {
//create 1st array for D&C, length = 1/2 a.length
Comparable[] first = new Comparable[a.length / 2];
//create 2nd array for D&C, length = a.length - first.length
Comparable[] second = new Comparable[a.length - first.length];
//source/position: a[0], destination/position: first[0], length: first.length
System.arraycopy(a, 0, first, 0, first.length);
//source/position: a[first.length], destination/pos: second[0], length: second.length
System.arraycopy(a, first.length, second, 0, second.length);
//recursively sort the first half
first = mergeSort(first);
//recursively sort second half
second = mergeSort(second);
//merge the halves
a = merge(first, second);
}
//return the merged array
return a;
}
public static Comparable[] merge(Comparable[] a, Comparable[] b) {
Comparable[] result = new Comparable[a.length + b.length];
//Index Position in first array - starting with first element
int first = 0;
//Index Position in second array - starting with first element
int second = 0;
//Index Position in merged array - starting with first position
int merged = 0;
//Compare first and second,
while (first < a.length && second < b.length) {
if (a[first].compareTo(b[second]) < 0) {
result[merged++] = a[first++];
} else {
result[merged++] = b[second++];
}
}
//Store remaining elements of 1st array
while (first < a.length) {
result[merged++] = a[first++];
}
//Store remaining elements of second array
while (second < b.length) {
result[merged++] = b[second++];
}
//copy elements from both halves - each half will have the sorted elements
// System.arraycopy(a, first, result, merged, a.length - first);
// System.arraycopy(b, second, result, merged, b.length - second);
return result;
}
【问题讨论】:
-
您是否意识到 Java 是按值传递的,即重新分配参数对调用者不可见?所以
a = mergeSort(a);可能没有你期望的效果。 -
我之前已经删除了它,它仍然在运行而不进行排序,但是在我现在使用的代码中,我已经删除了它,它仍然没有对任何东西进行排序。
-
好吧,删除那段代码并不意味着解决问题,因为您首先丢失了一段代码。您需要在某处返回排序后的数组才能看到更改。
-
我能够通过在我的方法中创建一个新对象来正确实现它,以解决正在发生的引用问题,以防止排序后的数组返回到 main