Skip to content

Commit a97ed1b

Browse files
committed
modify code
1 parent 5cf23a4 commit a97ed1b

2 files changed

Lines changed: 153 additions & 57 deletions

File tree

src/class08/Code02_MergeSort.java

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ public static void mergeSort1(int[] arr) {
1010
process(arr, 0, arr.length - 1);
1111
}
1212

13+
// arr[L...R]范围上,请让这个范围上的数,有序!
1314
public static void process(int[] arr, int L, int R) {
1415
if (L == R) {
1516
return;
1617
}
18+
// int mid = (L + R) / 2
1719
int mid = L + ((R - L) >> 1);
1820
process(arr, L, mid);
1921
process(arr, mid + 1, R);
@@ -28,6 +30,8 @@ public static void merge(int[] arr, int L, int M, int R) {
2830
while (p1 <= M && p2 <= R) {
2931
help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++];
3032
}
33+
// 要么p1越界,要么p2越界
34+
// 不可能出现:共同越界
3135
while (p1 <= M) {
3236
help[i++] = arr[p1++];
3337
}
@@ -39,31 +43,70 @@ public static void merge(int[] arr, int L, int M, int R) {
3943
}
4044
}
4145

42-
// 非递归方法实现
4346
public static void mergeSort2(int[] arr) {
4447
if (arr == null || arr.length < 2) {
4548
return;
4649
}
50+
int step = 1;
4751
int N = arr.length;
48-
int mergeSize = 1;
49-
while (mergeSize < N) {
52+
while (step < N) {
5053
int L = 0;
5154
while (L < N) {
52-
if (mergeSize >= N - L) {
55+
int M = 0;
56+
if (N - L >= step) {
57+
M = L + step - 1;
58+
} else {
59+
M = N - 1;
60+
}
61+
if (M == N - 1) {
5362
break;
5463
}
55-
int M = L + mergeSize - 1;
56-
int R = M + Math.min(mergeSize, N - M - 1);
64+
int R = 0;
65+
if (N - 1 - M >= step) {
66+
R = M + step;
67+
} else {
68+
R = N - 1;
69+
}
5770
merge(arr, L, M, R);
58-
L = R + 1;
71+
if (R == N - 1) {
72+
break;
73+
} else {
74+
L = R + 1;
75+
}
5976
}
60-
if (mergeSize > N / 2) {
77+
if (step > N / 2) {
6178
break;
6279
}
63-
mergeSize <<= 1;
80+
step *= 2;
6481
}
82+
6583
}
6684

85+
// 非递归方法实现
86+
// public static void mergeSort2(int[] arr) {
87+
// if (arr == null || arr.length < 2) {
88+
// return;
89+
// }
90+
// int N = arr.length;
91+
// int mergeSize = 1;
92+
// while (mergeSize < N) {
93+
// int L = 0;
94+
// while (L < N) {
95+
// if (mergeSize >= N - L) {
96+
// break;
97+
// }
98+
// int M = L + mergeSize - 1;
99+
// int R = M + Math.min(mergeSize, N - M - 1);
100+
// merge(arr, L, M, R);
101+
// L = R + 1;
102+
// }
103+
// if (mergeSize > N / 2) {
104+
// break;
105+
// }
106+
// mergeSize <<= 1;
107+
// }
108+
// }
109+
67110
// for test
68111
public static int[] generateRandomArray(int maxSize, int maxValue) {
69112
int[] arr = new int[(int) ((maxSize + 1) * Math.random())];

src/class08/Code03_PartitionAndQuickSort.java

Lines changed: 101 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,107 @@
11
package class08;
22

3+
import java.util.Stack;
4+
35
public 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

Comments
 (0)