【发布时间】:2013-11-14 21:19:09
【问题描述】:
根据关于快速排序算法的在线资源,我重构了以下函数:
void quickSort(int *array, int arrayLength, int first, int last) {
int pivot, j, i, temp;
if (first < last) {
pivot = first;
i = first;
j = last;
while (i < j) {
while (array[i] <= array[pivot] && i < last) {
i++;
}
while (array[j] > array[pivot]) {
j--;
}
if (i < j) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
temp = array[pivot];
array[pivot] = array[j];
array[j] = temp;
quickSort(array, arrayLength, first, j-1);
quickSort(array, arrayLength, j+1, last);
}
printBars(array, arrayLength);
}
为了看看它是如何发挥作用的,我编写了一个printBars 过程,它可以像这样打印数组的内容
int bars[] = {2, 4, 1, 8, 5, 9, 10, 7, 3, 6};
int barCount = 10;
printBars(bars, barCount);
我在前面提到的数组bars[]上运行quickSort后的最终结果就是这个图形
quickSort(bars, barCount, 1, 10);
我的问题:
-
10去哪儿了? - 为什么有
0作为值之一(原始数组没有它)?
【问题讨论】:
-
你不想
quickSort(bars, barCount, 0, 9)吗? -
糟糕...@NicoSchertler 请提交您的正确答案 :)