用快速排序法对数组进行从小到大排序
#include<iostream>
using namespace std;
int partition(int a[],int low,int high)
{
int pivot=a[low];
while(low<high)
{
while(low<high&&pivot<=a[high])--high;
a[low]=a[high];
while(low<high&&pivot>=a[low])++low;
a[high]=a[low];
}
a[low]=pivot;
return low;
}
void quicksort(int a[],int low,int high)
{
if(low<high)
{
int pivotpos=partition(a,low,high);
quicksort(a,low,pivotpos-1);
quicksort(a,pivotpos+1,high);
}
}
int main()
{
int a[10]={42,54 ,54 ,34, 56, 75 ,43 ,23, 67,75};
quicksort(a,0,9);
for(int i=0;i<10;i++)
{
cout<<a[i]<<endl;
}
return 0;
}