|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * ImageJ software for multidimensional image processing and analysis. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2014 - 2018 ImageJ developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | +package net.imagej.ops.coloc; |
| 30 | + |
| 31 | +/** |
| 32 | + * Sorts and <code>int[]</code> according to a custom comparator. |
| 33 | + * <p> |
| 34 | + * This is an implementation of introsort, i.e. it is stable because |
| 35 | + * it tries the quicksort algorithm first and falls back to the heap |
| 36 | + * sort when it detects an unfavorable execution path. |
| 37 | + * </p> |
| 38 | + * |
| 39 | + * @author Johannes Schindelin |
| 40 | + */ |
| 41 | +public class IntArraySorter { |
| 42 | + |
| 43 | + private final static int SORT_SIZE_THRESHOLD = 16; |
| 44 | + |
| 45 | + public static void sort(int[] array, IntComparator comparator) { |
| 46 | + introSort(array, comparator, 0, array.length, array.length); |
| 47 | + insertionSort(array, comparator); |
| 48 | + } |
| 49 | + |
| 50 | + private static void introSort(int[] array, IntComparator comparator, int begin, int end, int limit) |
| 51 | + { |
| 52 | + while (end - begin > SORT_SIZE_THRESHOLD) { |
| 53 | + if (limit == 0) { |
| 54 | + heapSort(array, comparator, begin, end); |
| 55 | + return; |
| 56 | + } |
| 57 | + limit >>= 1; |
| 58 | + |
| 59 | + // median of three |
| 60 | + int a = array[begin]; |
| 61 | + int b = array[begin + (end - begin) / 2 + 1]; |
| 62 | + int c = array[end - 1]; |
| 63 | + int median; |
| 64 | + if (comparator.compare(a, b) < 0) { |
| 65 | + median = comparator.compare(b, c) < 0 ? |
| 66 | + b : (comparator.compare(a, c) < 0 ? c : a); |
| 67 | + } else { |
| 68 | + median = comparator.compare(b, c) > 0 ? |
| 69 | + b : (comparator.compare(a, c) > 0 ? c : a); |
| 70 | + } |
| 71 | + |
| 72 | + // partition |
| 73 | + int pivot, i = begin, j = end; |
| 74 | + for (;;) { |
| 75 | + while (comparator.compare(array[i], median) < 0) { |
| 76 | + ++i; |
| 77 | + } |
| 78 | + --j; |
| 79 | + while (comparator.compare(median, array[j]) < 0) { |
| 80 | + --j; |
| 81 | + } |
| 82 | + if (i >= j) { |
| 83 | + pivot = i; |
| 84 | + break; |
| 85 | + } |
| 86 | + int swap = array[i]; |
| 87 | + array[i] = array[j]; |
| 88 | + array[j] = swap; |
| 89 | + ++i; |
| 90 | + } |
| 91 | + |
| 92 | + introSort(array, comparator, pivot, end, limit); |
| 93 | + end = pivot; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static void heapSort(int[] array, IntComparator comparator, |
| 98 | + int begin, int end) |
| 99 | + { |
| 100 | + int count = end - begin; |
| 101 | + for (int i = count / 2 - 1; i >= 0; --i) { |
| 102 | + siftDown(array, comparator, i, count, begin); |
| 103 | + } |
| 104 | + for (int i = count - 1; i > 0; --i) { |
| 105 | + // swap begin and begin + i |
| 106 | + int swap = array[begin + i]; |
| 107 | + array[begin + i] = array[begin]; |
| 108 | + array[begin] = swap; |
| 109 | + |
| 110 | + siftDown(array, comparator, 0, i, begin); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static void siftDown(int[] array, IntComparator comparator, |
| 115 | + int i, int count, int offset) |
| 116 | + { |
| 117 | + int value = array[offset + i]; |
| 118 | + while (i < count / 2) { |
| 119 | + int child = 2 * i + 1; |
| 120 | + if (child + 1 < count && |
| 121 | + comparator.compare(array[child], array[child + 1]) < 0) { |
| 122 | + ++child; |
| 123 | + } |
| 124 | + if (comparator.compare(value, array[child]) >= 0) { |
| 125 | + break; |
| 126 | + } |
| 127 | + array[offset + i] = array[offset + child]; |
| 128 | + i = child; |
| 129 | + } |
| 130 | + array[offset + i] = value; |
| 131 | + } |
| 132 | + |
| 133 | + private static void insertionSort(int[] array, |
| 134 | + IntComparator comparator) |
| 135 | + { |
| 136 | + for (int j = 1; j < array.length; ++j) { |
| 137 | + int t = array[j]; |
| 138 | + int i = j - 1; |
| 139 | + while (i >= 0 && comparator.compare(array[i], t) > 0) { |
| 140 | + array[i + 1] = array[i]; |
| 141 | + i = i - 1; |
| 142 | + } |
| 143 | + array[i + 1] = t; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments