1473N/A * Copyright 2009 Google Inc. All Rights Reserved. 1473N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1473N/A * This code is free software; you can redistribute it and/or modify it 1473N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 1473N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 1473N/A * This code is distributed in the hope that it will be useful, but WITHOUT 1473N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1473N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1473N/A * version 2 for more details (a copy is included in the LICENSE file that 1473N/A * You should have received a copy of the GNU General Public License version 1473N/A * 2 along with this work; if not, write to the Free Software Foundation, 1473N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 1473N/A * A stable, adaptive, iterative mergesort that requires far fewer than 1473N/A * n lg(n) comparisons when running on partially sorted arrays, while 1473N/A * offering performance comparable to a traditional mergesort when run 1473N/A * on random arrays. Like all proper mergesorts, this sort is stable and 1473N/A * runs O(n log n) time (worst case). In the worst case, this sort requires 1473N/A * temporary storage space for n/2 object references; in the best case, 1473N/A * it requires only a small constant amount of space. 1473N/A * This implementation was adapted from Tim Peters's list sort for 1473N/A * Python, which is described in detail here: 1473N/A * Tim's C code may be found here: 1473N/A * The underlying techniques are described in this paper (and may have 1473N/A * "Optimistic Sorting and Information Theoretic Complexity" 1473N/A * SODA (Fourth Annual ACM-SIAM Symposium on Discrete Algorithms), 1473N/A * pp 467-474, Austin, Texas, 25-27 January 1993. 1473N/A * While the API to this class consists solely of static methods, it is 1473N/A * (privately) instantiable; a TimSort instance holds the state of an ongoing 1473N/A * sort, assuming the input array is large enough to warrant the full-blown 1473N/A * TimSort. Small arrays are sorted in place, using a binary insertion sort. 1473N/A * This is the minimum sized sequence that will be merged. Shorter 1473N/A * sequences will be lengthened by calling binarySort. If the entire 1473N/A * array is less than this length, no merges will be performed. 1473N/A * This constant should be a power of two. It was 64 in Tim Peter's C 1473N/A * implementation, but 32 was empirically determined to work better in 1473N/A * this implementation. In the unlikely event that you set this constant 1473N/A * to be a number that's not a power of two, you'll need to change the 1473N/A * {@link #minRunLength} computation. 1473N/A * If you decrease this constant, you must change the stackLen 1473N/A * computation in the TimSort constructor, or you risk an 1473N/A * of the minimum stack length required as a function of the length 1473N/A * of the array being sorted and the minimum merge sequence length. 1473N/A * The comparator for this sort. 1473N/A * When we get into galloping mode, we stay there until both runs win less 1473N/A * often than MIN_GALLOP consecutive times. 1473N/A * This controls when we get *into* galloping mode. It is initialized 1473N/A * to MIN_GALLOP. The mergeLo and mergeHi methods nudge it higher for 1473N/A * random data, and lower for highly structured data. 1473N/A * Maximum initial size of tmp array, which is used for merging. The array 1473N/A * can grow to accommodate demand. 1473N/A * Unlike Tim's original C version, we do not allocate this much storage 1473N/A * when sorting smaller arrays. This change was required for performance. 1473N/A private T[]
tmp;
// Actual runtime type will be Object[], regardless of T 1473N/A * A stack of pending runs yet to be merged. Run i starts at 1473N/A * address base[i] and extends for len[i] elements. It's always 1473N/A * true (so long as the indices are in bounds) that: 1473N/A * runBase[i] + runLen[i] == runBase[i + 1] 1473N/A * so we could cut the storage for this, but it's a minor amount, 1473N/A * and keeping all the info explicit simplifies the code. 1473N/A * Creates a TimSort instance to maintain the state of an ongoing sort. 1473N/A * @param a the array to be sorted 1473N/A * @param c the comparator to determine the order of the sort 1473N/A // Allocate temp storage (which may be increased later if necessary) 1473N/A * Allocate runs-to-be-merged stack (which cannot be expanded). The 1473N/A * version always uses the same stack length (85), but this was 1473N/A * measured to be too expensive when sorting "mid-sized" arrays (e.g., 1473N/A * 100 elements) in Java. Therefore, we use smaller (but sufficiently 1473N/A * large) stack lengths for smaller arrays. The "magic numbers" in the 1473N/A * computation below must be changed if MIN_MERGE is decreased. See 1473N/A * the MIN_MERGE declaration above for more information. 1473N/A * The next two methods (which are package private and static) constitute 1473N/A * the entire API of this class. Each of these methods obeys the contract 1473N/A * of the public method with the same signature in java.util.Arrays. 1473N/A return;
// Arrays of size 0 and 1 are always sorted 1473N/A // If array is small, do a "mini-TimSort" with no merges 1473N/A * March over the array once, left to right, finding natural runs, 1473N/A * extending short natural runs to minRun elements, and merging runs 1473N/A * to maintain stack invariant. 1473N/A // If run is short, extend to min(minRun, nRemaining) 1473N/A // Push run onto pending-run stack, and maybe merge 1473N/A // Advance to find next run 1473N/A // Merge all remaining runs to complete sort 1473N/A * Sorts the specified portion of the specified array using a binary 1473N/A * insertion sort. This is the best method for sorting small numbers 1473N/A * of elements. It requires O(n log n) compares, but O(n^2) data 1473N/A * If the initial part of the specified range is already sorted, 1473N/A * this method can take advantage of it: the method assumes that the 1473N/A * elements from index {@code lo}, inclusive, to {@code start}, 1473N/A * exclusive are already sorted. 1473N/A * @param a the array in which a range is to be sorted 1473N/A * @param lo the index of the first element in the range to be sorted 1473N/A * @param hi the index after the last element in the range to be sorted 1473N/A * @param start the index of the first element in the range that is 3203N/A * not already known to be sorted ({@code lo <= start <= hi}) 1473N/A * @param c comparator to used for the sort 1473N/A // Set left (and right) to the index where a[start] (pivot) belongs 1473N/A * pivot >= all in [lo, left). 1473N/A * pivot < all in [right, start). 1473N/A * The invariants still hold: pivot >= all in [lo, left) and 1473N/A * pivot < all in [left, start), so pivot belongs at left. Note 1473N/A * that if there are elements equal to pivot, left points to the 1473N/A * first slot after them -- that's why this sort is stable. 3880N/A * Slide elements over to make room for pivot. 1473N/A // Switch is just an optimization for arraycopy in default case 1473N/A * Returns the length of the run beginning at the specified position in 1473N/A * the specified array and reverses the run if it is descending (ensuring 1473N/A * that the run will always be ascending when the method returns). 1473N/A * A run is the longest ascending sequence with: 1473N/A * a[lo] <= a[lo + 1] <= a[lo + 2] <= ... 1473N/A * or the longest descending sequence with: 1473N/A * a[lo] > a[lo + 1] > a[lo + 2] > ... 1473N/A * For its intended use in a stable mergesort, the strictness of the 1473N/A * definition of "descending" is needed so that the call can safely 1473N/A * reverse a descending sequence without violating stability. 1473N/A * @param a the array in which a run is to be counted and possibly reversed 1473N/A * @param lo index of the first element in the run 1473N/A * @param hi index after the last element that may be contained in the run. 3203N/A It is required that {@code lo < hi}. 1473N/A * @param c the comparator to used for the sort 1473N/A * @return the length of the run beginning at the specified position in 1473N/A // Find end of run, and reverse range if descending 1473N/A * Reverse the specified range of the specified array. 1473N/A * @param a the array in which a range is to be reversed 1473N/A * @param lo the index of the first element in the range to be reversed 1473N/A * @param hi the index after the last element in the range to be reversed 1473N/A * Returns the minimum acceptable run length for an array of the specified 1473N/A * length. Natural runs shorter than this will be extended with 1473N/A * Roughly speaking, the computation is: 1473N/A * If n < MIN_MERGE, return n (it's too small to bother with fancy stuff). 1473N/A * Else if n is an exact power of 2, return MIN_MERGE/2. 1473N/A * Else return an int k, MIN_MERGE/2 <= k <= MIN_MERGE, such that n/k 1473N/A * is close to, but strictly less than, an exact power of 2. 1473N/A * @param n the length of the array to be sorted 1473N/A * @return the length of the minimum run to be merged 1473N/A int r =
0;
// Becomes 1 if any 1 bits are shifted off 1473N/A * Pushes the specified run onto the pending-run stack. 1473N/A * @param runBase index of the first element in the run 1473N/A * @param runLen the number of elements in the run 1473N/A * Examines the stack of runs waiting to be merged and merges adjacent runs 1473N/A * until the stack invariants are reestablished: 1473N/A * 1. runLen[i - 3] > runLen[i - 2] + runLen[i - 1] 1473N/A * 2. runLen[i - 2] > runLen[i - 1] 1473N/A * This method is called each time a new run is pushed onto the stack, 1473N/A * so the invariants are guaranteed to hold for i < stackSize upon 1473N/A break;
// Invariant is established 1473N/A * Merges all runs on the stack until only one remains. This method is 1473N/A * called once, to complete the sort. 1473N/A * Merges the two runs at stack indices i and i+1. Run i must be 1473N/A * the penultimate or antepenultimate run on the stack. In other words, 1473N/A * i must be equal to stackSize-2 or stackSize-3. 1473N/A * @param i stack index of the first of the two runs to merge 1473N/A * Record the length of the combined runs; if i is the 3rd-last 1473N/A * run now, also slide over the last run (which isn't involved 1473N/A * in this merge). The current run (i+1) goes away in any case. 1473N/A * Find where the first element of run2 goes in run1. Prior elements 1473N/A * in run1 can be ignored (because they're already in place). 1473N/A * Find where the last element of run1 goes in run2. Subsequent elements 1473N/A * in run2 can be ignored (because they're already in place). 1473N/A // Merge remaining runs, using tmp array with min(len1, len2) elements 1473N/A * Locates the position at which to insert the specified key into the 1473N/A * specified sorted range; if the range contains an element equal to key, 1473N/A * returns the index of the leftmost equal element. 1473N/A * @param key the key whose insertion point to search for 1473N/A * @param a the array in which to search 1473N/A * @param base the index of the first element in the range 1473N/A * @param len the length of the range; must be > 0 1473N/A * @param hint the index at which to begin the search, 0 <= hint < n. 1473N/A * The closer hint is to the result, the faster this method will run. 1473N/A * @param c the comparator used to order the range, and to search 1473N/A * @return the int k, 0 <= k <= n such that a[b + k - 1] < key <= a[b + k], 1473N/A * pretending that a[b - 1] is minus infinity and a[b + n] is infinity. 1473N/A * In other words, key belongs at index b + k; or in other words, 1473N/A * the first k elements of a should precede key, and the last n - k 1473N/A // Gallop right until a[base+hint+lastOfs] < key <= a[base+hint+ofs] 1473N/A // Make offsets relative to base 1473N/A }
else {
// key <= a[base + hint] 1473N/A // Gallop left until a[base+hint-ofs] < key <= a[base+hint-lastOfs] 1473N/A // Make offsets relative to base 1473N/A * Now a[base+lastOfs] < key <= a[base+ofs], so key belongs somewhere 1473N/A * to the right of lastOfs but no farther right than ofs. Do a binary 1473N/A * search, with invariant a[base + lastOfs - 1] < key <= a[base + ofs]. 1473N/A * Like gallopLeft, except that if the range contains an element equal to 1473N/A * key, gallopRight returns the index after the rightmost equal element. 1473N/A * @param key the key whose insertion point to search for 1473N/A * @param a the array in which to search 1473N/A * @param base the index of the first element in the range 1473N/A * @param len the length of the range; must be > 0 1473N/A * @param hint the index at which to begin the search, 0 <= hint < n. 1473N/A * The closer hint is to the result, the faster this method will run. 1473N/A * @param c the comparator used to order the range, and to search 1473N/A * @return the int k, 0 <= k <= n such that a[b + k - 1] <= key < a[b + k] 1473N/A // Gallop left until a[b+hint - ofs] <= key < a[b+hint - lastOfs] 1473N/A // Make offsets relative to b 1473N/A }
else {
// a[b + hint] <= key 1473N/A // Gallop right until a[b+hint + lastOfs] <= key < a[b+hint + ofs] 1473N/A // Make offsets relative to b 1473N/A * Now a[b + lastOfs] <= key < a[b + ofs], so key belongs somewhere to 1473N/A * the right of lastOfs but no farther right than ofs. Do a binary 1473N/A * search, with invariant a[b + lastOfs - 1] <= key < a[b + ofs]. 1473N/A * Merges two adjacent runs in place, in a stable fashion. The first 1473N/A * element of the first run must be greater than the first element of the 1473N/A * second run (a[base1] > a[base2]), and the last element of the first run 1473N/A * (a[base1 + len1-1]) must be greater than all elements of the second run. 1473N/A * For performance, this method should be called only when len1 <= len2; 1473N/A * its twin, mergeHi should be called if len1 >= len2. (Either method 1473N/A * may be called if len1 == len2.) 1473N/A * @param base1 index of first element in first run to be merged 1473N/A * @param len1 length of first run to be merged (must be > 0) 1473N/A * @param base2 index of first element in second run to be merged 1473N/A * @param len2 length of second run to be merged (must be > 0) 1473N/A // Copy first run into temp array 1473N/A T[] a =
this.a;
// For performance 1473N/A // Move first element of second run and deal with degenerate cases 1473N/A int count1 =
0;
// Number of times in a row that first run won 1473N/A int count2 =
0;
// Number of times in a row that second run won 1473N/A * Do the straightforward thing until (if ever) one run starts 1473N/A * One run is winning so consistently that galloping may be a 1473N/A * huge win. So try that, and continue galloping until (if ever) 1473N/A * neither run appears to be winning consistently anymore. 1473N/A "Comparison method violates its general contract!");
1473N/A * Like mergeLo, except that this method should be called only if 1473N/A * len1 >= len2; mergeLo should be called if len1 <= len2. (Either method 1473N/A * may be called if len1 == len2.) 1473N/A * @param base1 index of first element in first run to be merged 1473N/A * @param len1 length of first run to be merged (must be > 0) 1473N/A * @param base2 index of first element in second run to be merged 1473N/A * @param len2 length of second run to be merged (must be > 0) 1473N/A // Copy second run into temp array 1473N/A T[] a =
this.a;
// For performance 1473N/A // Move last element of first run and deal with degenerate cases 1473N/A int count1 =
0;
// Number of times in a row that first run won 1473N/A int count2 =
0;
// Number of times in a row that second run won 1473N/A * Do the straightforward thing until (if ever) one run 1473N/A * appears to win consistently. 1473N/A * One run is winning so consistently that galloping may be a 1473N/A * huge win. So try that, and continue galloping until (if ever) 1473N/A * neither run appears to be winning consistently anymore. 1473N/A "Comparison method violates its general contract!");
1473N/A * Ensures that the external array tmp has at least the specified 1473N/A * number of elements, increasing its size if necessary. The size 1473N/A * increases exponentially to ensure amortized linear time complexity. 1473N/A * @param minCapacity the minimum required capacity of the tmp array 1473N/A * @return tmp, whether or not it grew 1473N/A // Compute smallest power of 2 > minCapacity 1473N/A * Checks that fromIndex and toIndex are in range, and throws an 1473N/A * appropriate exception if they aren't. 1473N/A * @param arrayLen the length of the array 1473N/A * @param fromIndex the index of the first element of the range 1473N/A * @param toIndex the index after the last element of the range 1473N/A * @throws IllegalArgumentException if fromIndex > toIndex 1473N/A * @throws ArrayIndexOutOfBoundsException if fromIndex < 0