有人知道怎么证明这个快排算法的正确性吗?
C++代码:void QuickSort(int a[], int left, int right)
{
if (left<right)
{
int pivotpos = Partition(a, left, right);
QuickSort(a, left, pivotpos-1);
QuickSort(a, pivotpos+1 ,right);
}
}
int Partition(int a[], int low, int high)
{
int pivot = median3(a, low ,high);
int i = low;
int j = high-1;
int temp;
while (1)
{
while (i<j && a[i]<pivot)
{
++i;
}
while (i<j && a[j]>pivot)
{
--j;
}
if (i<j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
++i;
--j;
}
else
break;
}
if (a[i]>pivot)
{
a[high] = a[i];
a[i] = pivot;
}
return i;
}
//取最左、最右和中间三个值作比较,使最左边存三者中最小,中间处存三者中最大
//并返回最右值作为基准值
int median3(int a[], int left, int right)
{
int k = left;
int mid = (left+right)/2;
int temp;
if (a[mid]<a[k])
{
k = mid;
}
if (a[right]<a[k])
{
k = right;
}
if (k!=left)
{
temp = a[k];
a[k] = a[left];
a[left] =temp;
}
if (a[right]>a[mid])
{
temp = a[right];
a[right] = a[mid];
a[mid] = temp;
}
return a[right];
}
不理解的地方就是Partition函数while(1)循环跳出后的那个if语句,它只做大于判断,那其它情况呢?