. 1 #include <the iostream> 2 #include <Vector> . 3 #include <algorithm> . 4 . 5 the using namespace STD; . 6 . 7 void quicksort (Vector < int > & A, int L, int R & lt) { . 8 // recursive outlet . 9 IF (L> = R & lt) return ; 10 // the left and right borders define two pointers, pointers left in the i <= target, the right pointer j> = target . 11 int I = L - . 1 , j = R & lt + . 1 ; 12 is // target current interval as [l, r] in any of a number, generally selected from l, r, l + r >> 2, the position of the random value 13 is int target A = [L + R & lt >> . 1 ]; 14 // implemented here left pointer i <= target, j right pointer> = target, the termination condition is not met even pointer 15 the while (i < J) { 16 // long as even a number of pointers to meet the condition is not satisfied, the stop position number, otherwise continue to walk . 17 do ++ I; the while (a [I] < target); 18 is do --j; the while (a [j]> target); . 19 // implemented here the exchange, because certain number satisfying> i position = target, j empathy same position, so They exchange, so that the number they want respectively 20 is IF ( I < J) the swap (a [I], a [J]); 21 is } 22 is // to sort recursive had been properly arranged two sections 23 quicksort(a,l,j); 24 quicksort(a,j+1,r); 25 } 26 27 int main(){ 28 int n; 29 cin >> n; 30 vector<int> a(n,0); 31 for(int i = 0;i < a.size();++i)cin>>a[i]; 32 quicksort(a,0,a.size()-1); 33 for(int i = 0;i < a.size();++i)cout << a[i] << " "; 34 return 0; 35 }
Quick sort merge sort
Guess you like
Origin www.cnblogs.com/sxq-study/p/12061178.html
Recommended
Ranking