Quick Sort

  

 1 #include <iostream>
 2 using namespace std;
 3 int partition(int *Ary,int low ,int high){
 4     int temp;
 5     int pivotkey=Ary[low];
 6     while(low<high){
 7         while(low<high && Ary[high]>=pivotkey){
 8             high--;
 9         }
10         temp=Ary[low];
11         Ary[low]=Ary[high];
12         Ary[high]=temp;
13         while(low<high && Ary[low]<=pivotkey){
14             low++;
15         }
16         temp=Ary[low];
17         Ary[low]=Ary[high];
18         Ary[high]=temp;
19     }
20     return low;
21 }
22 void Qsort(int *Ary,int low,int high){
23     if(low<high){
24         int key=partition(Ary,low,high);
25         
26         Qsort(Ary,low,key-1);
27         Qsort(Ary,key+1,high);
28     }
29     
30 }
31 int main(int argc, char *argv[]) {
32     
33     cout<<"Plz enter 10 numbers which then will be sorted"<<endl;
34     int Ary[10] ;
35     for (int i=0;i<10;i++){
36         cin>>Ary[i];
37     }
38     int length=(sizeof(Ary)/sizeof(int));
39     Qsort(Ary,0,length-1);
40     for(int j=0;j<10;j++){
41         cout<<Ary[j]<<" ";
42     }
43     return 0;
44     
45 }

 

posted @ 2018-10-14 15:33  Rohlf  阅读(142)  评论(0编辑  收藏  举报