1#include <iostream>2#include <random>3 using namespacestd;4 5 intPARTITION (int*Const,Const int&,Const int&);6 7 intRand_int (Const int&up,Const int&lo) {8 Random_device Rd;9 mt19937 Mt (RD ());TenUniform_int_distribution<>d (up,lo); One returnd (MT); A } - - voidSWAP2 (int&a,int&b) { the intt =A; -A =b; -b =T; - } + - voidQuick_sort (int*ConstAConst int&p,Const int&R) { + if(P <R) { A intQ =PARTITION (A, p, R); atQuick_sort (A, p, Q-1); -Quick_sort (A, q +1, R); - } - } - - intPARTITION (int*ConstAConst int&p,Const int&R) { in /*Uniform partitioning by random selection of intermediate elements*/ - intRandom_main =Rand_int (P, r); to swap2 (A[random_main], a[r]); + intx =A[r]; - inti = P-1; the for(intj = P; J < r;++j) { * if(A[j] <=x) { $++i;Panax Notoginseng swap2 (A[i], a[j]); - } the } +SWAP2 (A[i +1], a[r]); A returni +1; the } + - intMainvoid) $ { $ intA[] = {2,8,7,1,3,5,6,4 }; -Quick_sort (A,0, (End (a)-Begin (a)-1)); -System"Pause"); the return 0; -}
Quick Sort-algorithms_3th