快速排序算法
#include <stdio.h>void swap(int *L, int m,int n)
{
int temp=L[m];
L[m]=L[n];
L[n]=temp;
}
int partition(int *L,const int low, const int high)
{
int m=low+1,n=high;
int base=L[low];
if(high-low==1)
{
if(L[low]>L[high])
swap(L,low,high);
return high;
}
while(m<n)
{
while(m<n && L[m]<=base) m++;
while(n>low && L[n]>=base) n--;
if(n>m)
{
swap(L,m,n);
m++,n--;
}
}
swap(L,low,n);
return n;
}
void QuickSort(int *L,const int left,const int right)
{
if(left<right)
{
int pos=partition(L,left,right);
QuickSort(L,left,pos-1);
QuickSort(L,pos+1,right);
}
}
int main (int argc,char *argv[])
{
int k;
int a[]={33,12,1,56,8,45,89,7,34};
QuickSort(a,0,8);
for(k=0;k<=8;k++)
printf("%4d",a[k]);
printf("\n");
return 0;
}