//快速排序一次划分
int Partition(int r[], int first, int end)
{
int i=first; //初始化
int j=end;
int temp;
while (i<j){
while (i<j && r[i]<= r[j])
j--;//右侧扫描
if (i<j){
temp=r[i];//将较小记录交换到前面
r[i]=r[j];
r[j]=temp;
i++;
}
while (i<j && r[i]<= r[j])
i++;//左侧扫描
if (i<j){
temp=r[j];
r[j]=r[i];
r[i]=temp;//将较大记录交换到后面
j--;
}
}
return i;//i为轴值记录的最终位置
}
void QuickSort(int a[],int p,int r){
if (p<r){
int q=partion(a,p,r);
QuickSort(a,p,q-1);
QuickSort(a,q+1,r);
}
}
int main(){
int array[]={0,-2,11,-4,13,-5,14,-43};
QuickSort(array,0,7);
for(int i=0;i<=7;i++)
cout<<array[i]<<" ";
cout<<endl;
return 0;
}
快速排序
最新推荐文章于 2020-01-01 20:44:54 发布