| Modifier and Type | Field and Description |
|---|---|
| private T[] | a
The array being sorted. |
| private Comparator | c
The comparator for this sort. |
| private static final boolean | DEBUG
Asserts have been placed in if-statements for performance. |
| private static final int | INITIAL_TMP_STORAGE_LENGTH
Maximum initial size of tmp array, which is used for merging. |
| private static final int | MIN_GALLOP
When we get into galloping mode, we stay there until both runs win less often than MIN_GALLOP consecutive times. |
| private static final int | MIN_MERGE
This is the minimum sized sequence that will be merged. |
| private int | minGallop
This controls when we get *into* galloping mode. |
| private final int[] | |
| private final int[] | |
| private int | stackSize
A stack of pending runs yet to be merged. |
| private T[] | tmp
Temp storage for merges. |
| private int |
| Access | Constructor and Description |
|---|---|
| pack-priv | |
| private | TimSort(T[]
the array to be sorted a, Comparator<? super T> the comparator to determine the order of the sort c)Creates a TimSort instance to maintain the state of an ongoing sort. |
| Modifier and Type | Method and Description |
|---|---|
| private static <T> void | binarySort(T[]
the array in which a range is to be sorted a, int the index of the first element in the range to be sorted lo, int the index after the last element in the range to be sorted hi, int the index of the first element in the range that is not already known to be sorted (@code lo <= start <= hi} start, Comparator<? super T> comparator to used for the sort c)Sorts the specified portion of the specified array using a binary insertion sort. |
| private static <T> int | Returns: the length of the run beginning at the specified position in the specified arraythe array in which a run is to be counted and possibly reversed a, int index of the first element in the run lo, int index after the last element that may be contained in the run. It is required that @code{lo < hi}. hi, Comparator<? super T> the comparator to used for the sort c)Returns the length of the run beginning at the specified position in the specified array and reverses the run if it is descending (ensuring that the run will always be ascending when the method returns). |
| public void | |
| private T[] | Returns: tmp, whether or not it grewthe minimum required capacity of the tmp array minCapacity)Ensures that the external array tmp has at least the specified number of elements, increasing its size if necessary. |
| private static <T> int | Returns: the int k, 0 <= k <= n such that a[b + k - 1] < key <= a[b + k], pretending that a[b - 1] is minus infinity and a[b + n] is infinity. In other words, key belongs at index b + k; or in other words, the first k elements of a should precede key, and the last n - k should follow it.the key whose insertion point to search for key, T[] the array in which to search a, int the index of the first element in the range base, int the length of the range; must be > 0 len, int the index at which to begin the search, 0 <= hint < n. The closer hint is to the result, the faster this method
will run. hint, Comparator<? super T> the comparator used to order the range, and to search c)Locates the position at which to insert the specified key into the specified sorted range; if the range contains an element equal to key, returns the index of the leftmost equal element. |
| private static <T> int | Returns: the int k, 0 <= k <= n such that a[b + k - 1] <= key < a[b + k]the key whose insertion point to search for key, T[] the array in which to search a, int the index of the first element in the range base, int the length of the range; must be > 0 len, int the index at which to begin the search, 0 <= hint < n. The closer hint is to the result, the faster this method
will run. hint, Comparator<? super T> the comparator used to order the range, and to search c)Like gallopLeft, except that if the range contains an element equal to key, gallopRight returns the index after the rightmost equal element. |
| private void | mergeAt(int
stack index of the first of the two runs to merge i)Merges the two runs at stack indices i and i+1. |
| private void | mergeCollapse()
Examines the stack of runs waiting to be merged and merges adjacent runs until the stack invariants are reestablished: 1. |
| private void | |
| private void | mergeHi(int
index of first element in first run to be merged base1, int length of first run to be merged (must be > 0) len1, int index of first element in second run to be merged (must be aBase + aLen) base2, int length of second run to be merged (must be > 0) len2)Like mergeLo, except that this method should be called only if len1 >= len2; mergeLo should be called if len1 <= len2. |
| private void | mergeLo(int
index of first element in first run to be merged base1, int length of first run to be merged (must be > 0) len1, int index of first element in second run to be merged (must be aBase + aLen) base2, int length of second run to be merged (must be > 0) len2)Merges two adjacent runs in place, in a stable fashion. |
| private static int | Returns: the length of the minimum run to be mergedthe length of the array to be sorted n)Returns the minimum acceptable run length for an array of the specified length. |
| private void | pushRun(int
index of the first element in the run runBase, int the number of elements in the run runLen)Pushes the specified run onto the pending-run stack. |
| private static void | rangeCheck(int
the length of the array arrayLen, int the index of the first element of the range fromIndex, int the index after the last element of the range toIndex)Checks that fromIndex and toIndex are in range, and throws an appropriate exception if they aren't. |
| private static void | reverseRange(Object[]
the array in which a range is to be reversed a, int the index of the first element in the range to be reversed lo, int the index after the last element in the range to be reversed hi)Reverse the specified range of the specified array. |
| pack-priv static <T> void | |
| pack-priv static <T> void |
| a | back to summary |
|---|---|
| private T[] a The array being sorted. | |
| c | back to summary |
|---|---|
| private Comparator<? super T> c The comparator for this sort. | |
| DEBUG | back to summary |
|---|---|
| private static final boolean DEBUG Asserts have been placed in if-statements for performance. To enable them, set this field to true and enable them in VM with a command line flag. If you modify this class, please do test the asserts! | |
| INITIAL_TMP_STORAGE_LENGTH | back to summary |
|---|---|
| private static final int INITIAL_TMP_STORAGE_LENGTH Maximum initial size of tmp array, which is used for merging. The array can grow to accommodate demand. Unlike Tim's original C version, we do not allocate this much storage when sorting smaller arrays. This change was required for performance. | |
| MIN_GALLOP | back to summary |
|---|---|
| private static final int MIN_GALLOP When we get into galloping mode, we stay there until both runs win less often than MIN_GALLOP consecutive times. | |
| MIN_MERGE | back to summary |
|---|---|
| private static final int MIN_MERGE This is the minimum sized sequence that will be merged. Shorter sequences will be lengthened by calling binarySort. If the
entire array is less than this length, no merges will be performed.
This constant should be a power of two. It was 64 in Tim Peter's C implementation, but 32 was empirically determined to work
better in this implementation. In the unlikely event that you set this constant to be a number that's not a power of two,
you'll need to change the | |
| minGallop | back to summary |
|---|---|
| private int minGallop This controls when we get *into* galloping mode. It is reset to MIN_GALLOP before each sort. The mergeLo and mergeHi methods nudge it higher for random data, and lower for highly structured data. | |
| runBase | back to summary |
|---|---|
| private final int[] runBase | |
| runLen | back to summary |
|---|---|
| private final int[] runLen | |
| stackSize | back to summary |
|---|---|
| private int stackSize A stack of pending runs yet to be merged. Run i starts at address base[i] and extends for len[i] elements. It's always true (so long as the indices are in bounds) that: runBase[i] + runLen[i] == runBase[i + 1] so we could cut the storage for this, but it's a minor amount, and keeping all the info explicit simplifies the code. | |
| tmp | back to summary |
|---|---|
| private T[] tmp Temp storage for merges. | |
| tmpCount | back to summary |
|---|---|
| private int tmpCount | |
| TimSort | back to summary |
|---|---|
| pack-priv TimSort() | |
| TimSort | back to summary |
|---|---|
| private TimSort(T[] a, Comparator<? super T> c) Creates a TimSort instance to maintain the state of an ongoing sort.
| |
| binarySort | back to summary |
|---|---|
| private static <T> void binarySort(T[] a, int lo, int hi, int start, Comparator<? super T> c) Sorts the specified portion of the specified array using a binary insertion sort. This is the best method for sorting small
numbers of elements. It requires O(n log n) compares, but O(n^2) data movement (worst case).
If the initial part of the specified range is already sorted, this method can take advantage of it: the method assumes that
the elements from index
| |
| countRunAndMakeAscending | back to summary |
|---|---|
| private static <T> int countRunAndMakeAscending(T[] a, int lo, int hi, Comparator<? super T> c) Returns the length of the run beginning at the specified position in the specified array and reverses the run if it is descending (ensuring that the run will always be ascending when the method returns). A run is the longest ascending sequence with: a[lo] <= a[lo + 1] <= a[lo + 2] <= ... or the longest descending sequence with: a[lo] > a[lo + 1] > a[lo + 2] > ... For its intended use in a stable mergesort, the strictness of the definition of "descending" is needed so that the call can safely reverse a descending sequence without violating stability.
| |
| doSort | back to summary |
|---|---|
| public void doSort(T[] a, Comparator<T> c, int lo, int hi) | |
| ensureCapacity | back to summary |
|---|---|
| private T[] ensureCapacity(int minCapacity) Ensures that the external array tmp has at least the specified number of elements, increasing its size if necessary. The size increases exponentially to ensure amortized linear time complexity.
| |
| gallopLeft | back to summary |
|---|---|
| private static <T> int gallopLeft(T key, T[] a, int base, int len, int hint, Comparator<? super T> c) Locates the position at which to insert the specified key into the specified sorted range; if the range contains an element equal to key, returns the index of the leftmost equal element.
| |
| gallopRight | back to summary |
|---|---|
| private static <T> int gallopRight(T key, T[] a, int base, int len, int hint, Comparator<? super T> c) Like gallopLeft, except that if the range contains an element equal to key, gallopRight returns the index after the rightmost equal element.
| |
| mergeAt | back to summary |
|---|---|
| private void mergeAt(int i) Merges the two runs at stack indices i and i+1. Run i must be the penultimate or antepenultimate run on the stack. In other words, i must be equal to stackSize-2 or stackSize-3.
| |
| mergeCollapse | back to summary |
|---|---|
| private void mergeCollapse() Examines the stack of runs waiting to be merged and merges adjacent runs until the stack invariants are reestablished: 1. runLen[n - 2] > runLen[n - 1] + runLen[n] 2. runLen[n - 1] > runLen[n] where n is the index of the last run in runLen. This method has been formally verified to be correct after checking the last 4 runs. Checking for 3 runs results in an exception for large arrays. (Source: http://envisage-project.eu/proving-android-java-and-python-sorting-algorithm-is-broken-and-how-to-fix-it/) This method is called each time a new run is pushed onto the stack, so the invariants are guaranteed to hold for i < stackSize upon entry to the method. | |
| mergeForceCollapse | back to summary |
|---|---|
| private void mergeForceCollapse() Merges all runs on the stack until only one remains. This method is called once, to complete the sort. | |
| mergeHi | back to summary |
|---|---|
| private void mergeHi(int base1, int len1, int base2, int len2) Like mergeLo, except that this method should be called only if len1 >= len2; mergeLo should be called if len1 <= len2. (Either method may be called if len1 == len2.)
| |
| mergeLo | back to summary |
|---|---|
| private void mergeLo(int base1, int len1, int base2, int len2) Merges two adjacent runs in place, in a stable fashion. The first element of the first run must be greater than the first element of the second run (a[base1] > a[base2]), and the last element of the first run (a[base1 + len1-1]) must be greater than all elements of the second run. For performance, this method should be called only when len1 <= len2; its twin, mergeHi should be called if len1 >= len2. (Either method may be called if len1 == len2.)
| |
| minRunLength | back to summary |
|---|---|
| private static int minRunLength(int n) Returns the minimum acceptable run length for an array of the specified length. Natural runs shorter than this will be
extended with
| |
| pushRun | back to summary |
|---|---|
| private void pushRun(int runBase, int runLen) Pushes the specified run onto the pending-run stack.
| |
| rangeCheck | back to summary |
|---|---|
| private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) Checks that fromIndex and toIndex are in range, and throws an appropriate exception if they aren't.
| |
| reverseRange | back to summary |
|---|---|
| private static void reverseRange(Object[] a, int lo, int hi) Reverse the specified range of the specified array.
| |
| sort | back to summary |
|---|---|
| pack-priv static <T> void sort(T[] a, Comparator<? super T> c) | |
| sort | back to summary |
|---|---|
| pack-priv static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c) | |