通过这个题目我以为我了发现数据结构课本快排代码的一个bug。
书上的快排代码:
template <class T>
void quickSort(T a[], int leftEnd, int rightEnd)
{
// Sort a[leftEnd:rightEnd], a[rightEnd+1] >= a[leftEnd:rightEnd].
if (leftEnd >= rightEnd) return;
int leftCursor = leftEnd, // left-to-right cursor
rightCursor = rightEnd + 1; // right-to-left cursor
int x = a[rightCursor];
T pivot = a[leftEnd];
// swap elements >= pivot on left side
// with elements <= pivot on right side
while (true)
{
do
{
// find >= element on left side
leftCursor++;
} while (a[leftCursor] < pivot);
do
{
// find <= element on right side
rightCursor--;
} while (a[rightCursor] > pivot);
if (leftCursor >= rightCursor)