2 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
26 package intervalstore.nonc;
28 import intervalstore.api.IntervalI;
31 * A dual pivot quicksort for int[] where the int is a pointer to something for
32 * which the value needs to be checked. This class is not used; it was just an
33 * idea I was trying. But it is sort of cool, so I am keeping it in the package
34 * for possible future use.
36 * Adapted from Java 7 java.util.DualPivotQuicksort -- int[] only. The only
37 * difference is that wherever an a[] value is compared, we use val(a[i])
38 * instead of a[i] itself. Pretty straightforward. Could be adapted for general
39 * use. Why didn't they do this in Java?
41 * val(i) is just a hack here, of course. A more general implementation might
42 * use a Function call.
44 * Just thought it was cool that you can do this.
46 * @author Bob Hanson 2019.09.02
50 class IntervalEndSorter
53 private IntervalI[] intervals;
55 private int val(int i)
57 return intervals[i].getEnd();
65 * The maximum number of runs in merge sort.
67 private static final int MAX_RUN_COUNT = 67;
70 * The maximum length of run in merge sort.
72 private static final int MAX_RUN_LENGTH = 33;
75 * If the length of an array to be sorted is less than this constant,
76 * Quicksort is used in preference to merge sort.
78 private static final int QUICKSORT_THRESHOLD = 286;
81 * If the length of an array to be sorted is less than this constant,
82 * insertion sort is used in preference to Quicksort.
84 private static final int INSERTION_SORT_THRESHOLD = 47;
87 * Sorting methods for seven primitive types.
91 * Sorts the specified range of the array using the given workspace array
92 * slice if possible for merging
95 * the array to be sorted
97 * the index of the first element, inclusive, to be sorted
99 * the index of the last element, inclusive, to be sorted
101 * a workspace array (slice)
103 * origin of usable space in work array
105 * usable size of work array
107 void sort(int[] a, IntervalI[] intervals, int len)
109 this.intervals = intervals;
111 int left = 0, right = len - 1;
112 // Use Quicksort on small arrays
113 if (right - left < QUICKSORT_THRESHOLD)
115 sort(a, left, right, true);
120 * Index run[i] is the start of i-th run
121 * (ascending or descending sequence).
123 int[] run = new int[MAX_RUN_COUNT + 1];
127 // Check if the array is nearly sorted
128 for (int k = left; k < right; run[count] = k)
130 switch (Integer.signum(val(a[k + 1]) - val(a[k])))
134 while (++k <= right && val(a[k - 1]) <= val(a[k]))
139 while (++k <= right && val(a[k - 1]) >= val(a[k]))
141 for (int lo = run[count] - 1, hi = k; ++lo < --hi;)
150 for (int m = MAX_RUN_LENGTH; ++k <= right
151 && val(a[k - 1]) == val(a[k]);)
155 sort(a, left, right, true);
162 * The array is not highly structured,
163 * use Quicksort instead of merge sort.
165 if (++count == MAX_RUN_COUNT)
167 sort(a, left, right, true);
172 // Check special cases
173 // Implementation note: variable "right" is increased by 1.
174 if (run[count] == right++)
175 { // The last run contains one element
176 run[++count] = right;
179 { // The array is already sorted
183 // Determine alternation base for merge
185 for (int n = 1; (n <<= 1) < count; odd ^= 1)
188 // Use or create temporary array b for merging
189 int[] b; // temp array; alternates with a
190 int ao, bo; // array offsets from 'left'
191 int blen = right - left; // space needed for b
192 int[] work = new int[blen];
196 System.arraycopy(a, left, work, workBase, blen);
200 ao = workBase - left;
206 bo = workBase - left;
210 for (int last; count > 1; count = last)
212 for (int k = (last = 0) + 2; k <= count; k += 2)
214 int hi = run[k], mi = run[k - 1];
215 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i)
217 if (q >= hi || p < mi && val(a[p + ao]) <= val(a[q + ao]))
219 b[i + bo] = a[p++ + ao];
223 b[i + bo] = a[q++ + ao];
228 if ((count & 1) != 0)
230 for (int i = right, lo = run[count - 1]; --i >= lo; b[i + bo] = a[i
245 * Sorts the specified range of the array by Dual-Pivot Quicksort.
248 * the array to be sorted
250 * the index of the first element, inclusive, to be sorted
252 * the index of the last element, inclusive, to be sorted
254 * indicates if this part is the leftmost in the range
256 private void sort(int[] a, int left, int right, boolean leftmost)
258 int length = right - left + 1;
260 // Use insertion sort on tiny arrays
261 if (length < INSERTION_SORT_THRESHOLD)
266 * Traditional (without sentinel) insertion sort,
267 * optimized for server VM, is used in case of
270 for (int i = left, j = i; i < right; j = ++i)
273 while (val(ai) < val(a[j]))
287 * Skip the longest ascending sequence.
295 } while (val(a[++left]) >= val(a[left - 1]));
298 * Every element from adjoining part plays the role
299 * of sentinel, therefore this allows us to avoid the
300 * left range check on each iteration. Moreover, we use
301 * the more optimized algorithm, so called pair insertion
302 * sort, which is faster (in the context of Quicksort)
303 * than traditional implementation of insertion sort.
305 for (int k = left; ++left <= right; k = ++left)
307 int a1 = a[k], a2 = a[left];
309 if (val(a1) < val(a2))
314 while (val(a1) < val(a[--k]))
320 while (val(a2) < val(a[--k]))
328 while (val(last) < val(a[--right]))
330 a[right + 1] = a[right];
337 // Inexpensive approximation of length / 7
338 int seventh = (length >> 3) + (length >> 6) + 1;
341 * Sort five evenly spaced elements around (and including) the
342 * center element in the range. These elements will be used for
343 * pivot selection as described below. The choice for spacing
344 * these elements was empirically determined to work well on
345 * a wide variety of inputs.
347 int e3 = (left + right) >>> 1; // The midpoint
348 int e2 = e3 - seventh;
349 int e1 = e2 - seventh;
350 int e4 = e3 + seventh;
351 int e5 = e4 + seventh;
353 // Sort these elements using insertion sort
354 if (val(a[e2]) < val(a[e1]))
361 if (val(a[e3]) < val(a[e2]))
366 if (val(t) < val(a[e1]))
372 if (val(a[e4]) < val(a[e3]))
389 if (val(a[e5]) < val(a[e4]))
413 int less = left; // The index of the first element of center part
414 int great = right; // The index before the first element of right part
416 if (val(a[e1]) != val(a[e2]) && val(a[e2]) != val(a[e3])
417 && val(a[e3]) != val(a[e4]) && val(a[e4]) != val(a[e5]))
420 * Use the second and fourth of the five sorted elements as pivots.
421 * These values are inexpensive approximations of the first and
422 * second terciles of the array. Note that pivot1 <= pivot2.
424 int pivot1 = val(a[e2]);
425 int pivot2 = val(a[e4]);
430 * The first and the last elements to be sorted are moved to the
431 * locations formerly occupied by the pivots. When partitioning
432 * is complete, the pivots are swapped back into their final
433 * positions, and excluded from subsequent sorting.
439 * Skip elements, which are less or greater than pivot values.
441 while (val(a[++less]) < pivot1)
443 while (val(a[--great]) > pivot2)
449 * left part center part right part
450 * +--------------------------------------------------------------+
451 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
452 * +--------------------------------------------------------------+
459 * all in (left, less) < pivot1
460 * pivot1 <= all in [less, k) <= pivot2
461 * all in (great, right) > pivot2
463 * Pointer k is the first index of ?-part.
465 outer: for (int k = less - 1; ++k <= great;)
468 if (val(ak) < pivot1)
469 { // Move a[k] to left part
472 * Here and below we use "a[i] = b; i++;" instead
473 * of "a[i++] = b;" due to performance issue.
478 else if (val(ak) > pivot2)
479 { // Move a[k] to right part
480 while (val(a[great]) > pivot2)
487 if (val(a[great]) < pivot1)
488 { // a[great] <= pivot2
494 { // pivot1 <= a[great] <= pivot2
498 * Here and below we use "a[i] = b; i--;" instead
499 * of "a[i--] = b;" due to performance issue.
506 // Swap pivots into their final positions
507 a[left] = a[less - 1];
508 a[less - 1] = pivot1k;
509 a[right] = a[great + 1];
510 a[great + 1] = pivot2k;
512 // Sort left and right parts recursively, excluding known pivots
513 sort(a, left, less - 2, leftmost);
514 sort(a, great + 2, right, false);
517 * If center part is too large (comprises > 4/7 of the array),
518 * swap internal pivot values to ends.
520 if (less < e1 && e5 < great)
523 * Skip elements, which are equal to pivot values.
525 while (val(a[less]) == pivot1)
530 while (val(a[great]) == pivot2)
538 * left part center part right part
539 * +----------------------------------------------------------+
540 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
541 * +----------------------------------------------------------+
548 * all in (*, less) == pivot1
549 * pivot1 < all in [less, k) < pivot2
550 * all in (great, *) == pivot2
552 * Pointer k is the first index of ?-part.
554 outer: for (int k = less - 1; ++k <= great;)
557 if (val(ak) == pivot1)
558 { // Move a[k] to left part
563 else if (val(ak) == pivot2)
564 { // Move a[k] to right part
565 while (val(a[great]) == pivot2)
572 if (val(a[great]) == pivot1)
573 { // a[great] < pivot2
576 * Even though a[great] equals to pivot1, the
577 * assignment a[less] = pivot1 may be incorrect,
578 * if a[great] and pivot1 are floating-point zeros
579 * of different signs. Therefore in float and
580 * double sorting methods we have to use more
581 * accurate assignment a[less] = a[great].
587 { // pivot1 < a[great] < pivot2
596 // Sort center part recursively
597 sort(a, less, great, false);
601 { // Partitioning with one pivot
603 * Use the third of the five sorted elements as pivot.
604 * This value is inexpensive approximation of the median.
606 int pivot = val(a[e3]);
609 * Partitioning degenerates to the traditional 3-way
610 * (or "Dutch National Flag") schema:
612 * left part center part right part
613 * +-------------------------------------------------+
614 * | < pivot | == pivot | ? | > pivot |
615 * +-------------------------------------------------+
622 * all in (left, less) < pivot
623 * all in [less, k) == pivot
624 * all in (great, right) > pivot
626 * Pointer k is the first index of ?-part.
628 for (int k = less; k <= great; ++k)
630 if (val(a[k]) == pivot)
636 { // Move a[k] to left part
642 { // a[k] > pivot - Move a[k] to right part
643 while (val(a[great]) > pivot)
647 if (val(a[great]) < pivot)
648 { // a[great] <= pivot
654 { // a[great] == pivot
656 * Even though a[great] equals to pivot, the
657 * assignment a[k] = pivot may be incorrect,
658 * if a[great] and pivot are floating-point
659 * zeros of different signs. Therefore in float
660 * and double sorting methods we have to use
661 * more accurate assignment a[k] = a[great].
665 // Actually, we do need a[great] for IntervalStore,
666 // because here, two, the numbers are not necessarily the same item
677 * Sort left and right parts recursively.
678 * All elements from center part are equal
679 * and, therefore, already sorted.
681 sort(a, left, less - 1, leftmost);
682 sort(a, great + 1, right, false);