快速排序
1 #include<iostream> 2 using namespace std; 3 4 void qsort(int *arr,int low,int high){ 5 int i=low; 6 int j=high; 7 if(i>=j) 8 return; 9 int tmp=arr[low]; 10 while(i<j){ 11 while(i<j&&arr[j]>tmp) 12 j--; 13 arr[i]=arr[j]; 14 while(i<j&&arr[i]<tmp) 15 i++; 16 arr[j]=arr[i]; 17 } 18 arr[i]=tmp; 19 qsort(arr,low,i-1); 20 qsort(arr,i+1,high); 21 } 22 int main(){ 23 int arr[5]={3,1,2,4,5}; 24 for(int i=0;i<5;i++){ 25 cout<<arr[i]<<","; 26 } 27 cout<<endl; 28 qsort(arr,0,4); 29 for(int i=0;i<5;i++){ 30 cout<<arr[i]<<","; 31 } 32 cout<<endl; 33 return 0; 34 }