1 #include<iostream> 2 using namespace std; 3 4 void QuickSort(int a[],int first,int last) 5 { 6 if(first==last) 7 return; 8 int i=first,j=last; 9 int tmp; 10 tmp=(a[first]+a[last])/2; 11 while(i!=j) 12 { 13 while((i<j)&&(a[i]<tmp)) 14 i++; 15 if(i<j) 16 { 17 swap(a[i],a[j]); 18 j--; 19 } 20 } 21 22 if((a[i]>=tmp)&&(i>=first)) 23 { 24 QuickSort(a,first,i-1); 25 QuickSort(a,i,last); 26 } 27 else if((a[i]>=tmp)&&(i<=last)) 28 { 29 QuickSort(a,first,i); 30 QuickSort(a,i+1,last); 31 } 32 } 33 34 void main() 35 { 36 int a[8]={25,-5,288,-139,45,196}; 37 int first=0,last=7; 38 39 QuickSort(a,first,last); 40 41 for(int i=0;i<8;i++) 42 cout<<a[i]<<" "; 43 }
这个快速排序,是把首位和末位的数据求平均加以比较,其中采用从左向右的顺序实现的。