标签:
1 #include <iostream> 2 #include <random> 3 using namespace std; 4 5 int PARTITION(int *const, const int &, const int &); 6 7 int rand_int(const int &up,const int &lo){ 8 random_device rd; 9 mt19937 mt(rd()); 10 uniform_int_distribution<> d(up,lo); 11 return d(mt); 12 } 13 14 void swap2(int &a, int &b){ 15 int t = a; 16 a = b; 17 b = t; 18 } 19 20 void QUICK_SORT(int *const A, const int &p, const int &r){ 21 if (p < r){ 22 int q = PARTITION(A, p, r); 23 QUICK_SORT(A, p, q - 1); 24 QUICK_SORT(A, q + 1, r); 25 } 26 } 27 28 int PARTITION(int *const A, const int &p, const int &r){ 29 /*通过随机选取中间元来进行均匀划分*/ 30 int random_main = rand_int(p, r); 31 swap2(A[random_main], A[r]); 32 int x = A[r]; 33 int i = p - 1; 34 for (int j = p; j < r;++j){ 35 if (A[j] <= x){ 36 ++i; 37 swap2(A[i], A[j]); 38 } 39 } 40 swap2(A[i + 1], A[r]); 41 return i + 1; 42 } 43 44 int main(void) 45 { 46 int a[] = { 2, 8, 7, 1, 3, 5, 6, 4 }; 47 QUICK_SORT(a, 0, (end(a) - begin(a) - 1)); 48 system("pause"); 49 return 0; 50 }
标签:
原文地址:http://www.cnblogs.com/lhyz/p/4306099.html