功力不够,准确快速实现一个快速排序不容易,关键是那些边界的>=和>一下子控制不好。
void quickSort(int a[], int l, int r)
{
int lKeeper = l;
int rKeeper = r;
int key = a[l];
int keyIndex = l;
if (lKeeper >= rKeeper)
return;
while (l < r)
{
while (a[r] >= key && r > l)
--r;
if (l != r)
{
swap(a[l], a[r]);
keyIndex = r;
}
while (a[l] <= key && l < r)
++l;
if (l != r)
{
swap(a[l], a[r]);
keyIndex = l;
}
}
quickSort(a, lKeeper, keyIndex - 1);
quickSort(a, keyIndex + 1, rKeeper);
}