首先,我假设这是学术性的,而不是实用性的,因为您没有使用内置的排序功能。话虽如此,这里有一些帮助您朝着正确的方向前进:
通常,可以将合并排序视为两种不同的方法:merge() 函数将两个排序列表合并为一个排序列表,mergeSort() 函数递归地将列表分解为单个元素列表。由于已经对单个元素列表进行了排序,因此您可以将所有列表合并为一个大的排序列表。
这是一些临时的伪代码:
merge(A, B):
C = empty list
While A and B are not empty:
If the first element of A is smaller than the first element of B:
Remove first element of A.
Add it to the end of C.
Otherwise:
Remove first element of B.
Add it to the end of C.
If A or B still contains elements, add them to the end of C.
mergeSort(A):
if length of A is 1:
return A
Split A into two lists, L and R.
Q = merge(mergeSort(L), mergeSort(R))
return Q
也许这会帮助你弄清楚你想去哪里。
如果没有,维基百科上总是有MergeSort。
附加:
为了帮助您,这里有一些在您的代码中内联的 cmets。
public static void mergeSort(int[] array, int left, int lHigh, int right, int rHigh) {
// what do lHigh and rHigh represent?
int elements = (rHigh - lHigh +1) ;
int[] temp = new int[elements];
int num = left;
// what does this while loop do **conceptually**?
while ((left <= lHigh) && (right <= rHigh)){
if (a[left] <= a[right]) {
// where is 'pos' declared or defined?
temp[pos] = a[left];
// where is leftLow declared or defined? Did you mean 'left' instead?
leftLow ++;
}
else {
temp[num] = a[right];
right ++;
}
num++;
}
// what does this while loop do **conceptually**?
while (left <= right){
// At this point, what is the value of 'num'?
temp[num] = a[left];
left += 1;
num += 1;
}
while (right <= rHigh) {
temp[num] = a[right];
right += 1;
num += 1;
}
// Maybe you meant a[i] = temp[i]?
for (int i=0; i < elements; i++){
// what happens if rHigh is less than elements at this point? Could
// rHigh ever become negative? This would be a runtime error if it did
a[rHigh] = temp[rHigh];
rHigh -= 1;
}
我故意含糊其辞,所以你考虑一下算法。尝试将您自己的 cmets 插入代码中。如果你能写出概念上发生的事情,那么你可能不需要 Stack Overflow :)
我的想法是您没有正确实施。这是因为看起来您只接触了数组的元素一次(或接近一次)。这意味着您有O(N) 的最坏情况,排序通常至少需要O(N * log N),据我所知,合并排序的更简单版本实际上是O(N^2)。
更多:
在最简单的归并排序实现中,我希望在 mergeSort() 方法中看到某种recursion。这是因为归并排序通常是递归定义的。有一些方法可以使用 for 和 while 循环迭代地完成此操作,但我绝对不推荐它作为学习工具,除非您以递归方式获得它。
老实说,我建议使用我的伪代码或您可能在 wikipedia 文章中找到的伪代码来实现这一点并从您的代码开始。如果您这样做后仍然无法正常工作,请在此处发布,我们将帮助您解决问题。
干杯!
最后:
// Precondition: array[left..lHigh] is sorted and array[right...rHigh] is sorted.
// Postcondition: array[left..rHigh] contains the same elements of the above parts, sorted.
public static void mergeSort(int[] array, int left, int lHigh, int right, int rHigh) {
// temp[] needs to be as large as the number of elements you're sorting (not half!)
//int elements = (rHigh - lHigh +1) ;
int elements = rHigh - left;
int[] temp = new int[elements];
// this is your index into the temp array
int num = left;
// now you need to create indices into your two lists
int iL = left;
int iR = right;
// Pseudo code... when you code this, make use of iR, iL, and num!
while( temp is not full ) {
if( left side is all used up ) {
copy rest of right side in.
make sure that at the end of this temp is full so the
while loop quits.
}
else if ( right side is all used up) {
copy rest of left side in.
make sure that at the end of this temp is full so the
while loop quits.
}
else if (array[iL] < array[iR]) { ... }
else if (array[iL] >= array[iR]) { ... }
}
}