#include "iostream.h" void order_1(int[],int,int); void main() { int len; int a[]={12,2,13,111,14,15,1,2}; len=sizeof(a)/sizeof(int); order_1(a,0,len-1); cout<<endl<<"result is:"; for(int k=0;k<len;k++) { cout<<a[k]<<" "; } } void order_1(int a[],int left,int right) //快速排序法 { int mid,l,r,temp; l=left; r=right; mid=a[(left+right)/2]; //取中值作为分界线
while(l<r) { while(a[l]<mid) ++l; while(a[r]>mid) --r;
if(l>=r) break;
temp=a[l]; a[l]=a[r]; a[r]=temp; if(l!=mid) --r; if(r!=mid) ++l; } if(l==r) l++; if(left<r) order_1(a,left,l-1); if(l<right) order_1(a,r+1,right); }