-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntArrays.java
More file actions
executable file
·350 lines (305 loc) · 10.9 KB
/
IntArrays.java
File metadata and controls
executable file
·350 lines (305 loc) · 10.9 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package src;
/** Contains static methods for dealing with int arrays
--sorting, searching, etc.-- together with the methods
that they use.
*/
public class IntArrays {
/** = index of x in b ---or the length of b if x is not in b. */
public static int linearSearch(int[] b, int x) {
// invariant: x is not in b[0..i-1]
int i;
for (i= 0; i != b.length && x != b[i]; i= i+1) {}
return i;
}
/** Assume virtual elements b[-1] = -infinity and b.[b.length] = +infinity.<br>
Return a value i that satisfies R: b[i] <= x < b[i+1] */
public static int binarySearch(int[] b, int x) {
int i= -1; int j= b.length;
// {P:b[i] <= x < b[j] and -1 <= i < j <= b.length}, i.e.
// 0--------i------------j-------- b.length
// post: b | |<=x| ? |>x| |
// -------------------------------
while (j != i+1) {
int e= (i+j)/2;
// {-1 <= i < e < j <= b.length}
if (b[e] <= x) i= e;
else j= e;
}
return i;
}
/** Return the position of the minimum value of b[h..k].<br>
Precondition: h < k. */
public static int min(int[] b, int h, int k) {
int p= h; // will contain index of minimum
int i= h;
// {inv: b[p] is the minimum of b[h..i]}, i.e.
//
// h-------p------------i---------k
// b | b[p] is min of this | ? |
// --------------------------------
while (i!= k) {
i= i+1;
if (b[i] < b[p])
{p= i;}
}
return p;
}
/** Sort b --put its elements in ascending order. */
public static void selectionSort(int[] b) {
int j= 0;
// {inv P: b[0..j-1] is sorted and b[0..j-1] <= b[j..]}, i.e.
// 0---------------j--------------- b.length
// inv : b | sorted, <= | >= |
// --------------------------------
while (j != b.length) {
int p= min(b, j, b.length-1);
// {b[p] is minimum of b[j..b.length-1]}
// Swap b[j] and b[p]
int t= b[j]; b[j]= b[p]; b[p]= t;
j= j+1;
}
}
/** Sort b --put its elements in ascending order. */
public static void selectionSort1(int[] b) {
int j= 0;
// {inv P: b[0..j-1] is sorted and b[0..j-1] <= b[j..]}
// 0---------------j--------------- b.length
// inv : b | sorted, <= | >= |
// --------------------------------
while (j != b.length) {
// Put into p the index of smallest element in b[j..]
int p= j;
for (int i= j+1; i != b.length; i++) {
if (b[i] < b[p]) p= i;
}
// Swap b[j] and b[p]
int t= b[j]; b[j]= b[p]; b[p]= t;
j= j+1;
}
}
/** Precondition: b[h..k-1] is sorted, and b[k] contains a value.<br>
Sort b[h..k] */
public static void insertValue(int[] b, int h, int k) {
int v= b[k];
int i= k;
/* inv P: (1) Placing v in b[i] makes b[h..k] a
permutation of its initial value
(2) b[h..k] with b[i] removed is initial b[h..k-1]
(3) v < b[i+1..k]
*/
while ((i != h) && v < b[i-1]) {
b[i]= b[i-1];
i= i-1;
}
b[i]= v;
}
/** Sort b[h..k] --put its elements in ascending order. */
public static void insertionSort(int[] b, int h, int k) {
// inv: h <= j <= k+1 and b[h..j-1] is sorted
// h---------------j-------------k
// inv : b | sorted, | ? |
// -------------------------------
for (int j= h; j <= k; j++) {
// Sort b[h..j], given that b[h..j-1] is sorted
insertValue(b,h,j);
}
}
/** b[h..k] has at least three elements.<br>
Let x be the value initially in b[h].<br>
Permute b[h..k] and return integer j satisfying R:<br><br>
b[h..j-1] <= b[j] = x <= b[j+1..k]
*/
public static int partition0(int[] b, int h, int k) {
//
// h---------------------------k
// pre: b |x| ? | for some x
// -----------------------------
//
// h---------------------------k
// post: b | <= x |x| >= x |
// -----------------------------
int j= h;
int i= k;
// {inv P: b[h+1..i-1] <= b[h] = x <= b[j+1..k]}, i.e.
//
// h---------j-------i------------k
// post: b | <= x |x| ? | >= x |
// --------------------------------
while (j < i) {
if (b[j+1] <= b[j]) {
int temp= b[j]; b[j]= b[j+1]; b[j+1]= temp;
j= j+1;
}
else {
int temp= b[j+1]; b[j+1]= b[i]; b[i]= temp;
i= i-1;
}
}
return j;
}
/** b[h..k] has at least three elements.<br>
Let x be the value initially in b[h].<br>
Permute b[h..k] and return integer j satisfying R:<br><br>
b[h..j-1] <= b[j] = x <= b[j+1..k]
*/
public static int partition(int[] b, int h, int k) {
// {Q: Let x be the value initially in b[h]}
int j;
// Truthify R1: b[h+1..j] <= b[h] = x <= b[j+1..k];
int i= h+1; j= k;
// {inv P: b[h+1..i-1] <= b[h] = x <= b[j+1..k]}, i.e.
//
//
// h---------i------j----------k
// b |x| <= x | ? | >= x |
// -----------------------------
while (i <= j) {
if (b[i] < b[h]) i= i+1;
else if (b[j] > b[h]) j= j-1;
else {// {b[j] < x < b[i]}
int t1= b[i]; b[i]= b[j]; b[j]= t1;
i= i+1; j= j-1;
}
}
int t= b[h]; b[h]= b[j]; b[j]= t;
// {R}
return j;
}
/** Permute b[h], b[(h+k)/2], and b[k] to put their median in b[h]. */
public static void medianOf3(int[] b, int h, int k) {
int e= (h+k)/2; // index of middle element of array
int m; // index of median
// Return if b[h] is median; else store index of median in m
if (b[h] <= b[e]) {
if (b[h] >= b[k]) return;
// {b[h] is smallest of the three}
if (b[e] <= b[k]) m= e;
else m= k;
}
else {
if (b[h] <= b[k]) return;
// {b[h] is largest of the three}
if (b[e] <= b[k]) m= k;
else m= e;
}
int t= b[h]; b[h]= b[m]; b[m]= t;
}
/** Return a copy of array segment b[h..k]. */
public static int[] copy(int[] b, int h, int k) {
int[] c= new int[k+1-h];
// inv: b[h..i-1] has been copied to c[0..i-h-1]
for (int i= h; i != k+1; i++)
{c[i-h]= b[i];}
return c;
}
/** Segments b[h..e] and b[e+1..k] are already sorted.<br>
Permute their values so that b[h..k] is sorted.
*/
public static void merge (int b[], int h, int e, int k) {
int[] c= copy(b,h,e);
// {c is a copy of original b[h..e]}
int i= h; int j= e+1; int m= 0;
/* inv: b[h..i-1] contains its final, sorted values
b[j..k] remains to be transferred
c[m..e-h] remains to be transferred
*/
for (i= h; i != k+1; i++) {
if (j <= k && (m > e-h || b[j] <= c[m])) {
b[i]= b[j]; j= j+1;
} else {
b[i]= c[m]; m= m+1;
}
}
}
/** Sort b[h..k]. */
public static void mergeSort(int[] b, int h, int k) {
if (h >= k) return;
int e= (h+k)/2;
mergeSort(b, h, e); // Sort b[h..e]
mergeSort(b, e+1, k); // Sort b[e+1:k]
merge(b, h, e, k); // Merge the 2 segments
}
/** Sort b[h..k] */
public static void quickSort0(int[] b, int h, int k) {
if (k+1-h < 2) return;
int j= partition(b,h,k);
// b[h..j-1] <= b[j] <= b[j+1..k]
quickSort(b,h,j-1);
quickSort(b,j+1,k);
}
/** Sort b[h..k]. */
public static void quickSort(int[] b, int h, int k) {
int h1= h;
int k1= k;
// invariant: b[h..k] is sorted if b[h1..k1] is
while(true) {
if (k1-h1 < 10) {
insertionSort(b,h1,k1);
return;
}
medianOf3(b, h1, k1);
// {b[h1] is between b[(h1+k1)/2] and b[k1]}
int j= partition(b,h1,k1);
// b[h1..j-1] <= b[j] <= b[j+1..k1], i.e.
//
// h1--------j--------k1
// b | <= x |x| >= x | for some x
// ---------------------
if (j-h1 <= k1-j) {
quickSort(b,h1,j-1); //sort smaller segment recursively
// b[j+1..k1] remains to be sorted
h1= j+1;
}
else {
quickSort(b,j+1,k1); //sort smaller segment recursively
// b[h1..j-1] remains to be sorted
k1= j-1;
}
}
}
/** Swap the values of b so that the negative ones are
first, then the 0's, and finally the positive ones.
In the original problem, the negative values are red
balls, 0's are white balls, positive values are blue balls*/
public static void DutchNationalFlag(int[] b) {
// 0------------------------------------ b.length
// pre: b| ? |
// -------------------------------------
//
// 0------------------------------------ b.length
// post:b| <0 | = 0 | >0 |
// -------------------------------------
int h= 0;
int k= 0;
int t= b.length-1;
// 0-------h-------k------t------------- b.length
// inv :b| <0 | = 0 | ? | >0 |
// -------------------------------------
while (k <= t) {
if (b[k] < 0) {
int temp= b[k]; b[k]= b[h]; b[h]= temp;
h= h+1; k= k+1;
}
else if (b[k] == 0) {
k= k+1;
}
else {
int temp= b[k]; b[k]= b[t]; b[t]= temp;
t= t-1;
}
}
}
/** Return the values of b, separated by ", " and with
the whole list delimited by "[" and "]". */
public static String toString(int[] b) {
String res= "[";
// inv: res contains b[0..k-1], with "[" at
// beginning and values separated by ", "
for (int k= 0; k != b.length; k= k+1) {
if (k != 0)
res= res + ", ";
res= res + b[k];
}
return res + "]";
}
}