普通快速排序的两种实现
1 #include<iostream> 2 using namespace std; 3 4 int arr[10] = {1,9,2,5,4,6,3,8,7,1}; 5 6 void dump(int arr[],int cnt) 7 { 8 for(int i=0;i<cnt;i++){ 9 cout<<arr[i]<<" "; 10 } 11 cout<<endl; 12 } 13 14 15 void QSort(int arr[],int start,int end) 16 { 17 if(start>=end)return; 18 int i=start,j=end; 19 int key = arr[i]; 20 while(i<j) 21 { 22 while(i<j && arr[j] > key)j--; 23 arr[i] = arr[j]; 24 while(i<j && arr[i] <= key)i++; 25 arr[j] = arr[i]; 26 } 27 arr[i] = key; 28 QSort(arr,start,i-1); 29 QSort(arr,i+1,end); 30 } 31 32 void QSort1(int arr[],int start,int end) 33 { 34 int tmp; 35 if(start>=end)return; 36 int key=arr[end]; 37 int i = start; 38 for(int j=start;j<end;j++){ 39 if(arr[j]>=key){ 40 tmp=arr[j]; 41 arr[j]=arr[i]; 42 arr[i]=tmp; 43 i++; 44 } 45 } 46 arr[end]=arr[i]; 47 arr[i]=key; 48 QSort1(arr,start,i-1); 49 QSort1(arr,i+1,end); 50 } 51 52 int main() 53 { 54 QSort(arr,0,9); 55 dump(arr,10); 56 QSort1(arr,0,9); 57 dump(arr,10); 58 return 0; 59 }