1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.numbers.arrays;
18
19 import java.util.Arrays;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 final class Sorting {
35
36
37 private Sorting() {}
38
39
40
41
42
43
44
45
46 static void sort(double[] x, int left, int right) {
47 for (int i = left; ++i <= right;) {
48 final double v = x[i];
49
50 if (v < x[i - 1]) {
51 int j = i;
52 while (--j >= left && v < x[j]) {
53 x[j + 1] = x[j];
54 }
55 x[j + 1] = v;
56 }
57 }
58 }
59
60
61
62
63
64
65
66
67
68 static void sort3(double[] x, int a, int b, int c) {
69
70
71
72 final double u = x[a];
73 final double v = x[b];
74 final double w = x[c];
75 if (w < u) {
76 if (v < w) {
77 x[a] = v;
78 x[b] = w;
79 x[c] = u;
80 return;
81 }
82 if (u < v) {
83 x[a] = w;
84 x[b] = u;
85 x[c] = v;
86 return;
87 }
88
89 x[a] = w;
90 x[c] = u;
91 return;
92 }
93 if (v < u) {
94
95 x[a] = v;
96 x[b] = u;
97 return;
98 }
99 if (w < v) {
100
101 x[b] = w;
102 x[c] = v;
103 }
104
105 }
106
107
108
109
110
111
112
113
114
115
116
117 static void sort5(double[] x, int a, int b, int c, int d, int e) {
118
119
120
121
122
123
124
125
126 if (x[e] < x[b]) {
127 final double u = x[e];
128 x[e] = x[b];
129 x[b] = u;
130 }
131 if (x[d] < x[a]) {
132 final double v = x[d];
133 x[d] = x[a];
134 x[a] = v;
135 }
136
137 if (x[d] < x[b]) {
138 final double u = x[d];
139 x[d] = x[b];
140 x[b] = u;
141 }
142 if (x[c] < x[a]) {
143 final double v = x[c];
144 x[c] = x[a];
145 x[a] = v;
146 }
147
148 if (x[e] < x[c]) {
149 final double u = x[e];
150 x[e] = x[c];
151 x[c] = u;
152 }
153 if (x[b] < x[a]) {
154 final double v = x[b];
155 x[b] = x[a];
156 x[a] = v;
157 }
158
159 if (x[e] < x[d]) {
160 final double u = x[e];
161 x[e] = x[d];
162 x[d] = u;
163 }
164 if (x[c] < x[b]) {
165 final double v = x[c];
166 x[c] = x[b];
167 x[b] = v;
168 }
169
170 if (x[d] < x[c]) {
171 final double u = x[d];
172 x[d] = x[c];
173 x[c] = u;
174 }
175 }
176
177
178
179
180
181
182
183
184
185
186
187 static void lowerMedian4(double[] x, int a, int b, int c, int d) {
188
189 if (x[d] < x[b]) {
190 final double u = x[d];
191 x[d] = x[b];
192 x[b] = u;
193 }
194 if (x[c] < x[a]) {
195 final double v = x[c];
196 x[c] = x[a];
197 x[a] = v;
198 }
199
200
201 if (x[c] < x[b]) {
202 final double u = x[c];
203 x[c] = x[b];
204 x[b] = u;
205 } else if (x[b] < x[a]) {
206
207
208 final double xb = x[a];
209 x[a] = x[b];
210 x[b] = xb;
211
212
213 if (x[d] < xb) {
214 x[b] = x[d];
215
216 x[d] = x[c];
217 x[c] = xb;
218 }
219 }
220 }
221
222
223
224
225
226
227
228
229
230
231
232 static void upperMedian4(double[] x, int a, int b, int c, int d) {
233
234 if (x[d] < x[b]) {
235 final double u = x[d];
236 x[d] = x[b];
237 x[b] = u;
238 }
239 if (x[c] < x[a]) {
240 final double v = x[c];
241 x[c] = x[a];
242 x[a] = v;
243 }
244
245
246 if (x[b] > x[c]) {
247 final double u = x[c];
248 x[c] = x[b];
249 x[b] = u;
250 } else if (x[c] > x[d]) {
251
252
253 final double xc = x[d];
254 x[d] = x[c];
255 x[c] = xc;
256
257
258 if (x[a] > xc) {
259 x[c] = x[a];
260
261 x[a] = x[b];
262 x[b] = xc;
263 }
264 }
265 }
266
267
268
269
270
271
272
273
274 static void sort(int[] x, int left, int right) {
275 for (int i = left; ++i <= right;) {
276 final int v = x[i];
277
278 if (v < x[i - 1]) {
279 int j = i;
280 while (--j >= left && v < x[j]) {
281 x[j + 1] = x[j];
282 }
283 x[j + 1] = v;
284 }
285 }
286 }
287
288
289
290
291
292
293
294
295
296 static void sort3(int[] x, int a, int b, int c) {
297
298
299
300 final int u = x[a];
301 final int v = x[b];
302 final int w = x[c];
303 if (w < u) {
304 if (v < w) {
305 x[a] = v;
306 x[b] = w;
307 x[c] = u;
308 return;
309 }
310 if (u < v) {
311 x[a] = w;
312 x[b] = u;
313 x[c] = v;
314 return;
315 }
316
317 x[a] = w;
318 x[c] = u;
319 return;
320 }
321 if (v < u) {
322
323 x[a] = v;
324 x[b] = u;
325 return;
326 }
327 if (w < v) {
328
329 x[b] = w;
330 x[c] = v;
331 }
332
333 }
334
335
336
337
338
339
340
341
342
343
344
345 static void sort5(int[] x, int a, int b, int c, int d, int e) {
346
347
348
349
350
351
352
353
354 if (x[e] < x[b]) {
355 final int u = x[e];
356 x[e] = x[b];
357 x[b] = u;
358 }
359 if (x[d] < x[a]) {
360 final int v = x[d];
361 x[d] = x[a];
362 x[a] = v;
363 }
364
365 if (x[d] < x[b]) {
366 final int u = x[d];
367 x[d] = x[b];
368 x[b] = u;
369 }
370 if (x[c] < x[a]) {
371 final int v = x[c];
372 x[c] = x[a];
373 x[a] = v;
374 }
375
376 if (x[e] < x[c]) {
377 final int u = x[e];
378 x[e] = x[c];
379 x[c] = u;
380 }
381 if (x[b] < x[a]) {
382 final int v = x[b];
383 x[b] = x[a];
384 x[a] = v;
385 }
386
387 if (x[e] < x[d]) {
388 final int u = x[e];
389 x[e] = x[d];
390 x[d] = u;
391 }
392 if (x[c] < x[b]) {
393 final int v = x[c];
394 x[c] = x[b];
395 x[b] = v;
396 }
397
398 if (x[d] < x[c]) {
399 final int u = x[d];
400 x[d] = x[c];
401 x[c] = u;
402 }
403 }
404
405
406
407
408
409
410
411
412
413
414
415 static void lowerMedian4(int[] x, int a, int b, int c, int d) {
416
417 if (x[d] < x[b]) {
418 final int u = x[d];
419 x[d] = x[b];
420 x[b] = u;
421 }
422 if (x[c] < x[a]) {
423 final int v = x[c];
424 x[c] = x[a];
425 x[a] = v;
426 }
427
428
429 if (x[c] < x[b]) {
430 final int u = x[c];
431 x[c] = x[b];
432 x[b] = u;
433 } else if (x[b] < x[a]) {
434
435
436 final int xb = x[a];
437 x[a] = x[b];
438 x[b] = xb;
439
440
441 if (x[d] < xb) {
442 x[b] = x[d];
443
444 x[d] = x[c];
445 x[c] = xb;
446 }
447 }
448 }
449
450
451
452
453
454
455
456
457
458
459
460 static void upperMedian4(int[] x, int a, int b, int c, int d) {
461
462 if (x[d] < x[b]) {
463 final int u = x[d];
464 x[d] = x[b];
465 x[b] = u;
466 }
467 if (x[c] < x[a]) {
468 final int v = x[c];
469 x[c] = x[a];
470 x[a] = v;
471 }
472
473
474 if (x[b] > x[c]) {
475 final int u = x[c];
476 x[c] = x[b];
477 x[b] = u;
478 } else if (x[c] > x[d]) {
479
480
481 final int xc = x[d];
482 x[d] = x[c];
483 x[c] = xc;
484
485
486 if (x[a] > xc) {
487 x[c] = x[a];
488
489 x[a] = x[b];
490 x[b] = xc;
491 }
492 }
493 }
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509 static int insertionSortIndices(int[] x, int n) {
510
511 int unique = 0;
512
513 for (int i = 0; ++i < n;) {
514 final int v = x[i];
515 int j = unique;
516 if (v > x[j]) {
517
518 x[++unique] = v;
519 } else if (v < x[j]) {
520
521 do {
522 --j;
523 } while (j >= 0 && v < x[j]);
524
525
526 if (j < 0 || v != x[j]) {
527
528 for (int k = unique; k > j; --k) {
529 x[k + 1] = x[k];
530 }
531 x[j + 1] = v;
532 ++unique;
533 }
534 }
535 }
536 return unique + 1;
537 }
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553 static int sortIndices(int[] x, int n) {
554
555
556 final HashIndexSet set = HashIndexSet.create(n);
557 int i = 0;
558 int last = 0;
559 set.add(x[0]);
560 while (++i < n) {
561 final int v = x[i];
562 if (set.add(v)) {
563 x[++last] = v;
564 }
565 }
566 Arrays.sort(x, 0, ++last);
567 return last;
568 }
569 }