1 /** 2 * 排序算法之快速排序 3 **/ 4 #include <iostream> 5 #include <vector> 6 7 using namespace std; 8 9 void sortKuaiPai(vector<int>& vec, int low, int high) { 10 if (low >= high) return ; 11 12 int first = low; 13 int last = high; 14 int key = vec[first]; 15 16 while (first < last) { 17 while (first < last && vec[last] >= key) { 18 last--; 19 } 20 if (first < last) { 21 vec[first++] = vec[last]; 22 } 23 24 while (first < last && vec[first <= key]) 25 first++; 26 if (first < last) 27 vec[last--] = vec[first]; 28 } 29 vec[first] = key; 30 sortKuaiPai(vec, low, first- 1); 31 sortKuaiPai(vec, first + 1, high); 32 } 33 34 int main() { 35 vector<int> vec = {2,1,3,5,4}; 36 int len = vec.size() - 1; 37 sortKuaiPai(vec, 0, len); 38 for (int i = 0; i < vec.size(); ++i) { 39 cout << vec[i] << endl; 40 } 41 return 0; 42 }