我正在尝试在数组上使用快速排序,但是我不完全确定我做错了什么。我的顺序应该是
-2 0 1 4 7 9 11 12 15
但是我得到:
0 1 4 7 15 12 11 9 -2
这是我的分区代码:
int partition( int* a, int left, int right)
{
int pivot, leftPoint, rightPoint, temp;
pivot = a[left];
leftPoint = left;
rightPoint = right + 1;
while(rightPoint > leftPoint)
{
while(a[leftPoint] <= pivot && leftPoint <= right)
leftPoint ++;
while(a[rightPoint] > pivot)
rightPoint --;
temp = a[leftPoint];
a[leftPoint] = a[rightPoint];
a[rightPoint] = temp;
}
temp = a[left];
a[left] = a[rightPoint];
a[rightPoint] = temp;
return rightPoint;
}
有人可以在这里帮助解释我的算法有什么问题吗?
编辑:这是我的初始数组:
7 12 1 -2 0 15 4 11 9
我称快速排序为
quicksort(a, 0, 8);
这是我的快速排序的实现:
void quickSort( int a[], int low, int high)
{
int pivotPoint;
if(low < high)
{
// divide and conquer
pivotPoint = partition( a, low, high);
quickSort( a, low, pivotPoint);
quickSort( a, pivotPoint + 1, high);
}
}