1 // Quick sorting 2 3 # Include <iostream> 4 # Include <cstdlib> 5 Using Namespace STD; 6 7 Int Part ( Int S [], Int P,Int R) // Put the number greater than s [p] to one side, and the number smaller than s [p] to one side. 8 { 9 Int X = S [p]; 10 Int I = P + 1 ; 11 Int J = R; 12 While ( True ) 13 { 14 While (S [I] <X & I <= r) I ++ ; 15 While (S [J]> X & J> = 1 ) J -- ; 16 If (I> = J) 17 Break ; 18 Int Temp = S [I]; 19 S [I] = S [J]; 20 S [J] = Temp; 21 } 22 S [p] = S [J]; 23 S [J] = X; 24 Return J; 25 } 26 27 28 Void Quicksort ( Int S [], Int P, Int R) 29 { 30 If (P <R) 31 { 32 Int Q = Part (S, P, R ); 33 Quicksort (S, P, Q- 1 ); 34 Quicksort (S, q + 1 , R ); 35 } 36 } 37 38 Int Main () 39 { 40 Int S [] = { 1 , 5 , 3 , 8 , 4 , 10 , 5 }; 41 Int P = 0 ; 42 Int R = Sizeof S/ Sizeof * S- 1 ; 43 Cout < " Before sorting: " < Endl; 44 For ( Int I = 0 ; I <= r; I ++ ) 45 Cout <s [I] < " " ; 46 Cout < Endl; 47 Quicksort (S, P, R ); 48 Cout < " After sorting: " < Endl; 49 For (I = 0 ; I <= r; I ++ ) 50 Cout <s [I] < " " ; 51 Cout < Endl; 52 }