forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sort.java
301 lines (267 loc) · 8.5 KB
/
Sort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package sort;
import java.util.*;
import java.util.function.IntPredicate;
public class Sort {
static Random rnd = new Random(1);
public static void qSort(int[] a, int low, int high) {
if (low >= high)
return;
int separator = a[low + rnd.nextInt(high - low + 1)];
int i = low;
int j = high;
do {
while (a[i] < separator) ++i;
while (a[j] > separator) --j;
if (i > j)
break;
int t = a[i];
a[i] = a[j];
a[j] = t;
++i;
--j;
} while (i <= j);
qSort(a, low, j);
qSort(a, i, high);
}
public static void mergeSort(int[] a, int low, int high) {
if (high - low < 2)
return;
int mid = (low + high) >>> 1;
mergeSort(a, low, mid);
mergeSort(a, mid, high);
int[] b = Arrays.copyOfRange(a, low, mid);
for (int i = low, j = mid, k = 0; k < b.length; i++) {
if (j == high || b[k] <= a[j]) {
a[i] = b[k++];
} else {
a[i] = a[j++];
}
}
}
public static void mergeSort2(int[] a, int low, int high) {
int size = high - low;
if (size < 2)
return;
int mid = (low + high) >>> 1;
mergeSort2(a, low, mid);
mergeSort2(a, mid, high);
int[] b = new int[size];
int i = low;
int j = mid;
for (int k = 0; k < size; k++) {
if (i < mid && (j == high || a[i] <= a[j])) {
b[k] = a[i++];
} else {
b[k] = a[j++];
}
}
System.arraycopy(b, 0, a, low, size);
}
public static void inPlaceMergeSort(int[] a, int low, int high) {
if (low < high - 1) {
int mid = (low + high) >>> 1;
mergeSort(a, low, mid);
mergeSort(a, mid, high);
inPlaceMerge(a, low, mid, high);
}
}
// O(n*log(n)) complexity
static void inPlaceMerge(int[] a, int from, int mid, int to) {
if (from >= mid || mid >= to)
return;
if (to - from == 2) {
if (a[from] > a[mid])
swap(a, from, mid);
return;
}
final int firstCut;
final int secondCut;
if (mid - from > to - mid) {
firstCut = from + (mid - from) / 2;
secondCut = binarySearchFirstTrue(i -> a[i] >= a[firstCut], mid, to);
} else {
secondCut = mid + (to - mid) / 2;
firstCut = binarySearchFirstTrue(i -> a[i] > a[secondCut], from, mid);
}
if (mid != firstCut && mid != secondCut) {
rotate(a, firstCut, mid, secondCut);
}
mid = firstCut + (secondCut - mid);
inPlaceMerge(a, from, firstCut, mid);
inPlaceMerge(a, mid, secondCut, to);
}
static void swap(int[] a, int i, int j) {
int t = a[j];
a[j] = a[i];
a[i] = t;
}
static void rotate(int[] a, int first, int middle, int last) {
int next = middle;
while (first != next) {
swap(a, first++, next++);
if (next == last)
next = middle;
else if (first == middle)
middle = next;
}
}
static int binarySearchFirstTrue(IntPredicate predicate, int fromInclusive, int toExclusive) {
int lo = fromInclusive - 1;
int hi = toExclusive;
while (lo < hi - 1) {
int mid = (lo + hi) >>> 1;
if (!predicate.test(mid)) {
lo = mid;
} else {
hi = mid;
}
}
return hi;
}
public static void heapSort(int[] a) {
int n = a.length;
for (int i = n / 2 - 1; i >= 0; i--) pushDown(a, i, n);
while (n > 1) {
swap(a, 0, n - 1);
pushDown(a, 0, --n);
}
}
static void pushDown(int[] h, int pos, int size) {
while (true) {
int child = 2 * pos + 1;
if (child >= size)
break;
if (child + 1 < size && h[child + 1] > h[child])
child++;
if (h[pos] >= h[child])
break;
swap(h, pos, child);
pos = child;
}
}
public static void bubbleSort(int[] a) {
for (int i = 0; i + 1 < a.length; i++) {
for (int j = 0; j + 1 < a.length; j++) {
if (a[j] > a[j + 1]) {
swap(a, j, j + 1);
}
}
}
}
public static void selectionSort(int[] a) {
int n = a.length;
int[] p = new int[n];
for (int i = 0; i < n; i++) p[i] = i;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (a[p[i]] > a[p[j]]) {
swap(p, i, j);
}
}
}
int[] b = a.clone();
for (int i = 0; i < n; i++) a[i] = b[p[i]];
}
public static void insertionSort(int[] a) {
for (int i = 1; i < a.length; i++) {
for (int j = i; j > 0; j--) {
if (a[j - 1] > a[j]) {
swap(a, j - 1, j);
}
}
}
}
public static void countingSort(int[] a) {
int max = 0;
for (int x : a) {
max = Math.max(max, x);
}
int[] cnt = new int[max + 1];
for (int x : a) {
++cnt[x];
}
for (int i = 1; i < cnt.length; i++) {
cnt[i] += cnt[i - 1];
}
int n = a.length;
int[] b = new int[n];
for (int i = 0; i < n; i++) {
b[--cnt[a[i]]] = a[i];
}
System.arraycopy(b, 0, a, 0, n);
}
public static void radixSort(int[] a) {
final int d = 8;
final int w = 32;
int[] t = new int[a.length];
for (int p = 0; p < w / d; p++) {
// counting-sort
int[] cnt = new int[1 << d];
for (int i = 0; i < a.length; i++) ++cnt[((a[i] ^ Integer.MIN_VALUE) >>> (d * p)) & ((1 << d) - 1)];
for (int i = 1; i < cnt.length; i++) cnt[i] += cnt[i - 1];
for (int i = a.length - 1; i >= 0; i--)
t[--cnt[((a[i] ^ Integer.MIN_VALUE) >>> (d * p)) & ((1 << d) - 1)]] = a[i];
System.arraycopy(t, 0, a, 0, a.length);
}
}
// random test
public static void main(String[] args) {
Random rnd = new Random(1);
for (int step = 0; step < 1000; step++) {
int n = rnd.nextInt(10) + 1;
int[] a = rnd.ints(n, 0, 1000).toArray();
int[] s = a.clone();
Arrays.sort(s);
int[] b = a.clone();
bubbleSort(b);
if (!Arrays.equals(s, b))
throw new RuntimeException();
b = a.clone();
selectionSort(b);
if (!Arrays.equals(s, b))
throw new RuntimeException();
b = a.clone();
insertionSort(b);
if (!Arrays.equals(s, b))
throw new RuntimeException();
b = a.clone();
countingSort(b);
if (!Arrays.equals(s, b))
throw new RuntimeException();
b = a.clone();
qSort(b, 0, b.length - 1);
if (!Arrays.equals(s, b))
throw new RuntimeException();
}
for (int step = 0; step < 10; step++) {
int n = rnd.nextInt(50_000) + 100_000;
int[] a = step == 0 ? new int[n] : rnd.ints(n).toArray();
int[] s = a.clone();
Arrays.sort(s);
int[] b = a.clone();
qSort(b, 0, b.length - 1);
if (!Arrays.equals(s, b))
throw new RuntimeException();
b = a.clone();
mergeSort(b, 0, b.length);
if (!Arrays.equals(s, b))
throw new RuntimeException();
b = a.clone();
mergeSort2(b, 0, b.length);
if (!Arrays.equals(s, b))
throw new RuntimeException();
b = a.clone();
inPlaceMergeSort(b, 0, b.length);
if (!Arrays.equals(s, b))
throw new RuntimeException();
b = a.clone();
heapSort(b);
if (!Arrays.equals(s, b))
throw new RuntimeException();
b = a.clone();
radixSort(b);
if (!Arrays.equals(s, b))
throw new RuntimeException();
}
}
}