11package class08 ;
22
3+ import java .util .Stack ;
4+
35public class Code03_PartitionAndQuickSort {
46
7+ public static void splitNum1 (int [] arr ) {
8+ int lessEqualR = -1 ;
9+ int index = 0 ;
10+ int N = arr .length ;
11+ while (index < N ) {
12+ if (arr [index ] <= arr [N - 1 ]) {
13+ swap (arr , ++lessEqualR , index ++);
14+ } else {
15+ index ++;
16+ }
17+ }
18+ }
19+
20+ public static void splitNum2 (int [] arr ) {
21+ int N = arr .length ;
22+ int lessR = -1 ;
23+ int moreL = N - 1 ;
24+ int index = 0 ;
25+ // arr[N-1]
26+ while (index < moreL ) {
27+ if (arr [index ] < arr [N - 1 ]) {
28+ swap (arr , ++lessR , index ++);
29+ } else if (arr [index ] > arr [N - 1 ]) {
30+ swap (arr , --moreL , index );
31+ } else {
32+ index ++;
33+ }
34+ }
35+ swap (arr , moreL , N - 1 );
36+ }
37+
538 public static void swap (int [] arr , int i , int j ) {
639 int tmp = arr [i ];
740 arr [i ] = arr [j ];
841 arr [j ] = tmp ;
942 }
1043
11- public static int partition (int [] arr , int L , int R ) {
12- if (L > R ) {
13- return -1 ;
44+ // arr[L...R]范围上,拿arr[R]做划分值,
45+ // L....R < = >
46+ public static int [] partition (int [] arr , int L , int R ) {
47+ int lessR = L - 1 ;
48+ int moreL = R ;
49+ int index = L ;
50+ while (index < moreL ) {
51+ if (arr [index ] < arr [R ]) {
52+ swap (arr , ++lessR , index ++);
53+ } else if (arr [index ] > arr [R ]) {
54+ swap (arr , --moreL , index );
55+ } else {
56+ index ++;
57+ }
1458 }
15- if (L == R ) {
16- return L ;
59+ swap (arr , moreL , R );
60+ return new int [] { lessR + 1 , moreL };
61+ }
62+
63+ public static void quickSort1 (int [] arr ) {
64+ if (arr == null || arr .length < 2 ) {
65+ return ;
1766 }
18- int lessEqual = L - 1 ;
19- int index = L ;
20- while (index < R ) {
21- if (arr [index ] <= arr [R ]) {
22- swap (arr , index , ++lessEqual );
67+ process (arr , 0 , arr .length - 1 );
68+ }
69+
70+ public static void process (int [] arr , int L , int R ) {
71+ if (L >= R ) {
72+ return ;
73+ }
74+ int [] equalE = partition (arr , L , R );
75+ process (arr , L , equalE [0 ] - 1 );
76+ process (arr , equalE [1 ] + 1 , R );
77+ }
78+
79+ public static class Job {
80+ public int L ;
81+ public int R ;
82+
83+ public Job (int left , int right ) {
84+ L = left ;
85+ R = right ;
86+ }
87+ }
88+
89+ public static void quickSort2 (int [] arr ) {
90+ if (arr == null || arr .length < 2 ) {
91+ return ;
92+ }
93+ Stack <Job > stack = new Stack <>();
94+ stack .push (new Job (0 , arr .length - 1 ));
95+ while (!stack .isEmpty ()) {
96+ Job cur = stack .pop ();
97+ int [] equals = partition (arr , cur .L , cur .R );
98+ if (equals [0 ] > cur .L ) { // 有< 区域
99+ stack .push (new Job (cur .L , equals [0 ] - 1 ));
100+ }
101+ if (equals [1 ] < cur .R ) { // 有 > 区域
102+ stack .push (new Job (equals [1 ] + 1 , cur .R ));
23103 }
24- index ++;
25104 }
26- swap (arr , ++lessEqual , R );
27- return lessEqual ;
28105 }
29106
30107 public static int [] netherlandsFlag (int [] arr , int L , int R ) {
@@ -50,40 +127,6 @@ public static int[] netherlandsFlag(int[] arr, int L, int R) {
50127 return new int [] { less + 1 , more };
51128 }
52129
53- public static void quickSort1 (int [] arr ) {
54- if (arr == null || arr .length < 2 ) {
55- return ;
56- }
57- process1 (arr , 0 , arr .length - 1 );
58- }
59-
60- public static void process1 (int [] arr , int L , int R ) {
61- if (L >= R ) {
62- return ;
63- }
64- int M = partition (arr , L , R );
65- process1 (arr , L , M - 1 );
66- process1 (arr , M + 1 , R );
67- }
68-
69- public static void quickSort2 (int [] arr ) {
70- if (arr == null || arr .length < 2 ) {
71- return ;
72- }
73- process2 (arr , 0 , arr .length - 1 );
74- }
75-
76- // arr[L...R] 排有序,快排2.0方式
77- public static void process2 (int [] arr , int L , int R ) {
78- if (L >= R ) {
79- return ;
80- }
81- // [ equalArea[0] , equalArea[0]]
82- int [] equalArea = netherlandsFlag (arr , L , R );
83- process2 (arr , L , equalArea [0 ] - 1 );
84- process2 (arr , equalArea [1 ] + 1 , R );
85- }
86-
87130 public static void quickSort3 (int [] arr ) {
88131 if (arr == null || arr .length < 2 ) {
89132 return ;
@@ -154,22 +197,32 @@ public static void printArray(int[] arr) {
154197
155198 // for test
156199 public static void main (String [] args ) {
200+ // int[] arr = { 7, 1, 3, 5, 4, 5, 1, 4, 2, 4, 2, 4 };
201+ //
202+ // splitNum2(arr);
203+ // for (int i = 0; i < arr.length; i++) {
204+ // System.out.print(arr[i] + " ");
205+ // }
206+
157207 int testTime = 500000 ;
158208 int maxSize = 100 ;
159209 int maxValue = 100 ;
160210 boolean succeed = true ;
211+ System .out .println ("test begin" );
161212 for (int i = 0 ; i < testTime ; i ++) {
162213 int [] arr1 = generateRandomArray (maxSize , maxValue );
163214 int [] arr2 = copyArray (arr1 );
164215 int [] arr3 = copyArray (arr1 );
165216 quickSort1 (arr1 );
166217 quickSort2 (arr2 );
167218 quickSort3 (arr3 );
168- if (!isEqual (arr1 , arr2 ) || !isEqual (arr2 , arr3 )) {
219+ if (!isEqual (arr1 , arr2 ) || !isEqual (arr1 , arr3 )) {
220+ System .out .println ("Oops!" );
169221 succeed = false ;
170222 break ;
171223 }
172224 }
225+ System .out .println ("test end" );
173226 System .out .println (succeed ? "Nice!" : "Oops!" );
174227
175228 }
0 commit comments