注意点:
i是从start开始的;
++i和--j都放else里面;
记得先递归出口;
1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 5 // 快排 6 void sort(vector<int>& res, int start, int end) { 7 if (start >= end) return; 8 int mv = res[start]; 9 int i = start, j = end; 10 while (i < j) { 11 while (j > i) { 12 if (res[j] < mv) --j; 13 else { 14 res[i] = res[j]; ++i; break; 15 } 16 } 17 while (j > i) { 18 if (res[i] > mv) ++i; 19 else { 20 res[j] = res[i]; --j; break; 21 } 22 } 23 } 24 res[i] = mv; 25 sort(res, start, i-1); 26 sort(res, i+1, end); 27 } 28 29 int main() { 30 vector<int> init = { 3,2,6,7,1,3,5,100,38,101,0 }; 31 sort(init,0,init.size()-1); 32 for (int i = 0; i < init.size(); ++i) { 33 cout << init[i]<< " "; 34 } 35 return 0; 36 }