//快速排序的另一种方法:以最后一个元素为枢轴值,从前往后进行比较,
//那么一次快速排序后,枢轴左边的元素的相对位置不变。
#include<iostream>
using namespace std;
int Partition(int a[],int low,int high)//快速排序
{
int x = a[high];
int i = low -1;
for(int j = low;j<=high-1;++j)
{
if(a[j] <= x)
{
++i;
swap(a[i],a[j]);
}
}
swap(a[i+1],a[high]);
return i+1;
}
void QuickSort(int a[],int low,int high)
{
if(low<high)//边界条件,即递归跳出的条件
{
int pivot = Partition(a, low, high);//划分
QuickSort(a, low, pivot-1);//依次对两个子表进行递归调用
QuickSort(a, pivot+1, high);
}
}
void main()
{
int A[8] = {3,8,7,1,2,5,6,4};
QuickSort(A, 0, 7);
for(int i = 0;i<8;++i)
cout<<A[i]<<' ';
cout<<endl;
}