我被困在这种合并排序方法上。我不想递归地做,但我不能让这个为我工作。我认为需要做一个小的改变才能让它发挥作用。有什么建议么?
protected static void merge(long[] a, long[] workSpace, int lowPtr, int highPtr, int upperBound) {
int j = 0; // workspace index
int lowerBound = lowPtr;
int mid = highPtr-1;
int n = upperBound-lowerBound+1; // # of items
while(lowPtr <= mid && highPtr <= upperBound)
if(a[lowPtr] < a[highPtr] )
workSpace[j++] = a[lowPtr++];
else
workSpace[j++] = a[highPtr++];
while(lowPtr <= mid)
workSpace[j++] = a[lowPtr++];
while(highPtr <= upperBound)
workSpace[j++] = a[highPtr++];
for(j=0; j<n; j++)
a[lowerBound+j] = workSpace[j];