快速排序法
1 #include <iostream.h> 2 3 void Qsort(int a[],int low,int high) 4 { 5 if(low>=high) 6 return; 7 int first=low; 8 int last=high; 9 int key=a[first];/*用字表的第一个记录作为枢轴*/ 10 while(first<last) 11 { 12 while(first<last&&a[last]>=key) --last; 13 a[first]=a[last];/*将比第一个小的移到低端*/ 14 while(first<last&&a[first]<=key) ++first; 15 a[last]=a[first];/*将比第一个大的移到高端*/ 16 } 17 a[first]=key;/*枢轴记录到位*/ 18 Qsort(a,low,first-1); 19 Qsort(a,first+1,high); 20 } 21 int main() 22 { 23 int a[]={57,68,59,52,72,28,96,33,24}; 24 Qsort(a,0,sizeof(a)/sizeof(a[0])-1);/*这里原文第三个参数要减1否则内存泄露*/ 25 for(int i=0;i<sizeof(a)/sizeof(a[0]);i++) 26 { 27 cout << a[i] << " "; 28 } 29 return(0); 30 }/*参考数据结构p274(清华大学出版社,严蔚敏)*/