快速排序
采取分治的思想,确定每一次分治中间标杆的位置。
i从左往右直到找到比标杆小的
j从右往左直到找到比标杆大的
a[i]与a[j]交换
当j<=i时,把标杆和a[j]交换
以上确定了标杆位置
template<class Type>
int Partition (Type a[], int l, int r)
{
int i = l, j = r + 1;
Type x=a[l];
while (true) {
while (a[++i] <x && i<r);
while (a[- -j] >x);
if (i >= j) break;
Swap(a[i], a[j]);
}
a[l] = a[j];
a[j] = x;
return j;
}
void QuickSort (Type a[], int l, int r)
{
if (l<r) {
int mid=Partition(a,l,r);
QuickSort (a,l,mid-1); //对左半段排序
QuickSort (a,mid+1,r); //对右半段排序
}
}