#include<iostream>
using namespace std;
void display(int *p,int len)
{
for(int i=1;i<len;i++)
{
cout <<p[i] <<endl;
}
}
int partion(int *p,int low,int high)
{
int tmp=p[low];
while(low<high)
{
while((low<high)&&(p[high]>=tmp))
{
high--;
}
p[low]=p[high];
while((low<high)&&(p[low]<=tmp))
{
low++;
}
p[high]=p[low];
}
p[low]=tmp;
return low;
}
void quicksort(int *p,int low,int high)
{
if(low <high)
{
int mid=partion(p,low,high);
quicksort(p,mid+1,high);
quicksort(p,low,mid-1);
}
}
int main()
{
int a[11]={0,1,3,5,7,9,2,4,6,8,10};
cout <<"before sort:" <<endl;
display(a,11);
quicksort(a,1,10);
cout <<"after sort:" <<endl;
display(a,11);
return 0;
}
c/c++ 快速排序
于 2021-06-24 20:27:46 首次发布