/*JDK 1.8
*/ package java.util; /**
* A stable, adaptive, iterative mergesort that requires far fewer than
* n lg(n) comparisons when running on partially sorted arrays, while
* offering performance comparable to a traditional mergesort when run
* on random arrays. Like all proper mergesorts, this sort is stable and
* runs O(n log n) time (worst case). In the worst case, this sort requires
* temporary storage space for n/2 object references; in the best case,
* it requires only a small constant amount of space.
*
* This implementation was adapted from Tim Peters's list sort for
* Python, which is described in detail here:
*
* http://svn.python.org/projects/python/trunk/Objects/listsort.txt
*
* Tim's C code may be found here:
*
* http://svn.python.org/projects/python/trunk/Objects/listobject.c
*
* The underlying techniques are described in this paper (and may have
* even earlier origins):
*
* "Optimistic Sorting and Information Theoretic Complexity"
* Peter McIlroy
* SODA (Fourth Annual ACM-SIAM Symposium on Discrete Algorithms),
* pp 467-474, Austin, Texas, 25-27 January 1993.
*
* While the API to this class consists solely of static methods, it is
* (privately) instantiable; a TimSort instance holds the state of an ongoing
* sort, assuming the input array is large enough to warrant the full-blown
* TimSort. Small arrays are sorted in place, using a binary insertion sort.
*
* @author Josh Bloch
*/
class TimSort<T> {
/**
* 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
* {@link #minRunLength} computation.
*
* If you decrease this constant, you must change the stackLen
* computation in the TimSort constructor, or you risk an
* ArrayOutOfBounds exception. See listsort.txt for a discussion
* of the minimum stack length required as a function of the length
* of the array being sorted and the minimum merge sequence length.
*/
private static final int MIN_MERGE = 32; /**
* The array being sorted.
*/
private final T[] a; /**
* The comparator for this sort.
*/
private final Comparator<? super T> c; /**
* 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_GALLOP = 7; /**
* This controls when we get *into* galloping mode. It is initialized
* to MIN_GALLOP. The mergeLo and mergeHi methods nudge it higher for
* random data, and lower for highly structured data.
*/
private int minGallop = MIN_GALLOP; /**
* 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.
*/
private static final int INITIAL_TMP_STORAGE_LENGTH = 256; /**
* Temp storage for merges. A workspace array may optionally be
* provided in constructor, and if so will be used as long as it
* is big enough.
*/
private T[] tmp;
private int tmpBase; // base of tmp array slice
private int tmpLen; // length of tmp array slice /**
* 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.
*/
private int stackSize = 0; // Number of pending runs on stack
private final int[] runBase;
private final int[] runLen; /**
* Creates a TimSort instance to maintain the state of an ongoing sort.
*
* @param a the array to be sorted
* @param c the comparator to determine the order of the sort
* @param work a workspace array (slice)
* @param workBase origin of usable space in work array
* @param workLen usable size of work array
*/
private TimSort(T[] a, Comparator<? super T> c, T[] work, int workBase, int workLen) {
this.a = a;
this.c = c; // Allocate temp storage (which may be increased later if necessary)
int len = a.length;
// 确定临时数组的长度, 如果低于默认值256的2倍, 则空间大小为原始数组a的长度乘以2, 否则为默认长度
int tlen = (len < 2 * INITIAL_TMP_STORAGE_LENGTH) ?
len >>> 1 : INITIAL_TMP_STORAGE_LENGTH;
if (work == null || workLen < tlen || workBase + tlen > work.length) {
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
T[] newArray = (T[])java.lang.reflect.Array.newInstance
(a.getClass().getComponentType(), tlen);
tmp = newArray;
tmpBase = 0;
tmpLen = tlen;
}
else {
// 当指定的work数组不为空, 且workLen大于计算出的tlen的长度, 并且work数组的有效长度大于tlen的长度时, 使用指定的临时数组
tmp = work;
tmpBase = workBase;
tmpLen = workLen;
} /*
* Allocate runs-to-be-merged stack (which cannot be expanded). The
* stack length requirements are described in listsort.txt. The C
* version always uses the same stack length (85), but this was
* measured to be too expensive when sorting "mid-sized" arrays (e.g.,
* 100 elements) in Java. Therefore, we use smaller (but sufficiently
* large) stack lengths for smaller arrays. The "magic numbers" in the
* computation below must be changed if MIN_MERGE is decreased. See
* the MIN_MERGE declaration above for more information.
* The maximum value of 49 allows for an array up to length
* Integer.MAX_VALUE-4, if array is filled by the worst case stack size
* increasing scenario. More explanations are given in section 4 of:
* http://envisage-project.eu/wp-content/uploads/2015/02/sorting.pdf
*/
int stackLen = (len < 120 ? 5 :
len < 1542 ? 10 :
len < 119151 ? 24 : 49);
runBase = new int[stackLen];
runLen = new int[stackLen];
} /*
* The next method (package private and static) constitutes the
* entire API of this class.
*/ /**
* Sorts the given range, using the given workspace array slice
* for temp storage when possible. This method is designed to be
* invoked from public methods (in class Arrays) after performing
* any necessary array bounds checks and expanding parameters into
* the required forms.
*
* @param a the array to be sorted
* @param lo the index of the first element, inclusive, to be sorted
* @param hi the index of the last element, exclusive, to be sorted
* @param c the comparator to use
* @param work a workspace array (slice)
* @param workBase origin of usable space in work array
* @param workLen usable size of work array
* @since 1.8
*/
static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
T[] work, int workBase, int workLen) {
assert c != null && a != null && lo >= 0 && lo <= hi && hi <= a.length; int nRemaining = hi - lo; //总共的待排序的元素个数
if (nRemaining < 2)
return; // Arrays of size 0 and 1 are always sorted // If array is small, do a "mini-TimSort" with no merges
// 当元素个数小于7时, 使用折半插入排序, 因为插入排序对于元素个数少的数组更快。
if (nRemaining < MIN_MERGE) {
// initRunLen是初始的有序的元素的个数,而从索引位置lo + initRunLen开始, 后面为乱序的元素。 一种优化方法, lo + initRunLen之前的不用排序了
int initRunLen = countRunAndMakeAscending(a, lo, hi, c);
// 折半插入排序
binarySort(a, lo, hi, lo + initRunLen, c);
return;
} /**
* March over the array once, left to right, finding natural runs,
* extending short natural runs to minRun elements, and merging runs
* to maintain stack invariant.
*/
// 新建一个TimSort实例, 存储运行时的状态, 比如临时的run(一个run即一个有序的数组)
TimSort<T> ts = new TimSort<>(a, c, work, workBase, workLen);
// 查找一个minRun值, 小于minRun时使用折半插入排序,MIN_MERGE/2 <= minRun <= MIN_MERGE
int minRun = minRunLength(nRemaining);
do {
// Identify next run
// 找到下一个有序的数组的长度
int runLen = countRunAndMakeAscending(a, lo, hi, c); // If run is short, extend to min(minRun, nRemaining)
if (runLen < minRun) {
// 使用折半插入排序扩展数组, 使之达到minRun, 因为元素个数小于minRun, 折半插入排序更快速
int force = nRemaining <= minRun ? nRemaining : minRun;
binarySort(a, lo, lo + force, lo + runLen, c);
runLen = force;
} // Push run onto pending-run stack, and maybe merge
// 把这个有序数组压入栈中
ts.pushRun(lo, runLen);
/**
* 判断当
* runLen[i - 3] <= runLen[i - 2] + runLen[i - 1]
* 且 runLen[i-3] < runLen[i-1]时
* 或
* runLen[i - 2] <= runLen[i - 1]
* 合并较小的两个有序数组, 以达到最大的平衡(即每个数组大小基本相同)
*/
ts.mergeCollapse(); // Advance to find next run
lo += runLen;
nRemaining -= runLen;
} while (nRemaining != 0); // Merge all remaining runs to complete sort
assert lo == hi;
//合并剩余数组
ts.mergeForceCollapse();
assert ts.stackSize == 1;
} /**
* 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 {@code lo}, inclusive, to {@code start},
* exclusive are already sorted.
*
* @param a the array in which a range is to be sorted
* @param lo the index of the first element in the range to be sorted
* @param hi the index after the last element in the range to be sorted
* @param start the index of the first element in the range that is
* not already known to be sorted ({@code lo <= start <= hi})
* @param c comparator to used for the sort
*/
@SuppressWarnings("fallthrough")
private static <T> void binarySort(T[] a, int lo, int hi, int start,
Comparator<? super T> c) {
assert lo <= start && start <= hi;
// start之前的有序元素直接略过
if (start == lo)
start++;
// 从start到hi, 使用折半插入排序进行数组排序
for ( ; start < hi; start++) {
//待插入的元素
T pivot = a[start]; // Set left (and right) to the index where a[start] (pivot) belongs
// 从left到right, 找到插入位置
int left = lo;
int right = start;
assert left <= right;
/*
* Invariants:
* pivot >= all in [lo, left).
* pivot < all in [right, start).
*/
while (left < right) {
int mid = (left + right) >>> 1;
if (c.compare(pivot, a[mid]) < 0)
right = mid;
else
left = mid + 1;
}
// left即为最终的插入位置, 因为start>=lo && start <=hi, 所以最终一定会找到一个位置使得pivot>=a[mid], 因此最终一定是pivot >= right, 因此最终为left的位置, 即mid+1
assert left == right; /*
* The invariants still hold: pivot >= all in [lo, left) and
* pivot < all in [left, start), so pivot belongs at left. Note
* that if there are elements equal to pivot, left points to the
* first slot after them -- that's why this sort is stable.
* Slide elements over to make room for pivot.
*/
int n = start - left; // The number of elements to move
// Switch is just an optimization for arraycopy in default case
switch (n) {
case 2: a[left + 2] = a[left + 1]; // 如果待移动元素个数小于等于2则直接移动
case 1: a[left + 1] = a[left];
break;
default: System.arraycopy(a, left, a, left + 1, n); // 从left开始往后移, 然后把start位置的元素插入到原来的left的位置
}
a[left] = pivot;
}
} /**
* 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.
*
* @param a the array in which a run is to be counted and possibly reversed
* @param lo index of the first element in the run
* @param hi index after the last element that may be contained in the run.
It is required that {@code lo < hi}.
* @param c the comparator to used for the sort
* @return the length of the run beginning at the specified position in
* the specified array
*/
private static <T> int countRunAndMakeAscending(T[] a, int lo, int hi,
Comparator<? super T> c) {
assert lo < hi;
int runHi = lo + 1;
if (runHi == hi)
return 1; // lo < hi, 且lo + 1 = hi, 因此是有序且升序的, 直接返回 // Find end of run, and reverse range if descending
if (c.compare(a[runHi++], a[lo]) < 0) { // Descending
// 如果是降序的, 找到最长的有序降序序列的长度, 并且把序列倒置, 使之升序
while (runHi < hi && c.compare(a[runHi], a[runHi - 1]) < 0)
runHi++;
reverseRange(a, lo, runHi);
} else { // Ascending
// 如果是升序的, 同样找到最长的有序序列的长度
while (runHi < hi && c.compare(a[runHi], a[runHi - 1]) >= 0)
runHi++;
} // 返回有序序列的长度
return runHi - lo;
} /**
* Reverse the specified range of the specified array.
*
* @param a the array in which a range is to be reversed
* @param lo the index of the first element in the range to be reversed
* @param hi the index after the last element in the range to be reversed
*/
private static void reverseRange(Object[] a, int lo, int hi) {
hi--;
// 首尾倒置
while (lo < hi) {
Object t = a[lo];
a[lo++] = a[hi];
a[hi--] = t;
}
} /**
* Returns the minimum acceptable run length for an array of the specified
* length. Natural runs shorter than this will be extended with
* {@link #binarySort}.
*
* Roughly speaking, the computation is:
*
* If n < MIN_MERGE, return n (it's too small to bother with fancy stuff).
* Else if n is an exact power of 2, return MIN_MERGE/2.
* Else return an int k, MIN_MERGE/2 <= k <= MIN_MERGE, such that n/k
* is close to, but strictly less than, an exact power of 2.
*
* For the rationale, see listsort.txt.
*
* @param n the length of the array to be sorted
* @return the length of the minimum run to be merged
*/
private static int minRunLength(int n) {
assert n >= 0;
int r = 0; // Becomes 1 if any 1 bits are shifted off
while (n >= MIN_MERGE) {
// n&1是判断n是能否被2整除, 如果不能被2整除, 最后一个Bit位一定是1,则1&1为1, r = r | 1 为1
r |= (n & 1);
n >>= 1;
}
return n + r;
} /**
* Pushes the specified run onto the pending-run stack.
*
* @param runBase index of the first element in the run
* @param runLen the number of elements in the run
*/
// 把有序序列起始位置和长度放入栈中
private void pushRun(int runBase, int runLen) {
this.runBase[stackSize] = runBase;
this.runLen[stackSize] = runLen;
stackSize++;
} /**
* Examines the stack of runs waiting to be merged and merges adjacent runs
* until the stack invariants are reestablished:
*
* 1. runLen[i - 3] > runLen[i - 2] + runLen[i - 1]
* 2. runLen[i - 2] > runLen[i - 1]
*
* 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.
*/
// 判断合并栈顶的三个元素中较小的两个, 或如果第二个元素比第一个小, 则合并, 使栈中所有的序列大小达到近似相等
private void mergeCollapse() {
while (stackSize > 1) {
int n = stackSize - 2;
if (n > 0 && runLen[n-1] <= runLen[n] + runLen[n+1]) {
if (runLen[n - 1] < runLen[n + 1])
n--;
mergeAt(n);
} else if (runLen[n] <= runLen[n + 1]) {
mergeAt(n);
} else {
break; // Invariant is established
}
}
} /**
* Merges all runs on the stack until only one remains. This method is
* called once, to complete the sort.
*/
// 最后合并栈中所有的序列, 直到最后只剩一个有序序列
private void mergeForceCollapse() {
while (stackSize > 1) {
int n = stackSize - 2;
// 如果runLen[n-1] < runLen[n+1], 则合并较小的较小的runBase[n-1]和runBase[n], 否则合并runBase[n]和runBase[n+1]
if (n > 0 && runLen[n - 1] < runLen[n + 1])
n--;
mergeAt(n);
}
} /**
* 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.
*
* @param i stack index of the first of the two runs to merge
*/
private void mergeAt(int i) {
assert stackSize >= 2;
assert i >= 0;
assert i == stackSize - 2 || i == stackSize - 3; int base1 = runBase[i];
int len1 = runLen[i];
int base2 = runBase[i + 1];
int len2 = runLen[i + 1];
assert len1 > 0 && len2 > 0;
assert base1 + len1 == base2; /*
* Record the length of the combined runs; if i is the 3rd-last
* run now, also slide over the last run (which isn't involved
* in this merge). The current run (i+1) goes away in any case.
*/
runLen[i] = len1 + len2;
// 如果i是栈顶倒数第三个元素, 则最后i+1一定会合进i数组, 因此i+1的位置替换成i+2
if (i == stackSize - 3) {
runBase[i + 1] = runBase[i + 2];
runLen[i + 1] = runLen[i + 2];
}
stackSize--; /*
* Find where the first element of run2 goes in run1. Prior elements
* in run1 can be ignored (because they're already in place).
*/
// 找到run2的首元素在run1中的位置
int k = gallopRight(a[base2], a, base1, len1, 0, c);
assert k >= 0;
// 忽略k之前的序列, 因为已经有序, 减少比较次数
base1 += k;
len1 -= k;
if (len1 == 0)
return; /*
* Find where the last element of run1 goes in run2. Subsequent elements
* in run2 can be ignored (because they're already in place).
*/
// 找打run1的尾元素在run2中的位置
len2 = gallopLeft(a[base1 + len1 - 1], a, base2, len2, len2 - 1, c);
assert len2 >= 0;
// len2 == 0, 说明run1和run2已经是一个整体有序的序列了, 直接返回。
if (len2 == 0)
return; // Merge remaining runs, using tmp array with min(len1, len2) elements
if (len1 <= len2)
mergeLo(base1, len1, base2, len2);
else
mergeHi(base1, len1, base2, len2);
} /**
* 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.
*
* @param key the key whose insertion point to search for
* @param a the array in which to search
* @param base the index of the first element in the range
* @param len the length of the range; must be > 0
* @param hint the index at which to begin the search, 0 <= hint < n.
* The closer hint is to the result, the faster this method will run.
* @param c the comparator used to order the range, and to search
* @return 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.
*/
private static <T> int gallopLeft(T key, T[] a, int base, int len, int hint,
Comparator<? super T> c) {
assert len > 0 && hint >= 0 && hint < len;
int lastOfs = 0;
int ofs = 1;
if (c.compare(key, a[base + hint]) > 0) {
// 查找区间[base + hint, len], 查找使得a[base+hint+lastOfs] < key <= a[base+hint+ofs]成立的ofs值。
int maxOfs = len - hint;
// 向右查找, 最大可能Ofs为maxOfs-1, 即len - hint - 1, 即a[base + len - 1]
while (ofs < maxOfs && c.compare(key, a[base + hint + ofs]) > 0) {
// 记录上一次ofs的值, 存到lastOfs中
lastOfs = ofs;
// ofs乘以2再加1
ofs = (ofs << 1) + 1;
// 整数溢出
if (ofs <= 0)
ofs = maxOfs;
} // ofs最大值为maxOfs
if (ofs > maxOfs)
ofs = maxOfs; // Make offsets relative to base
// 之前的ofs和lastOfs都是相对于hint位置的, 现在把它重置为相对于base的位置
lastOfs += hint;
ofs += hint;
} else { // key <= a[base + hint]
// 从base+hint向前搜索,查找区间[base, base + hint], 直到找到ofs值使得a[base+hint-ofs] < key <= a[base+hint-lastOfs]
// maxOfs为hint+1, 而ofs < maxOfs, 因此当ofs = maxOfs -1 时, 比较到的最左边的元素为a[base + hint - hint] == a[base]
final int maxOfs = hint + 1;
while (ofs < maxOfs && c.compare(key, a[base + hint - ofs]) <= 0) {
lastOfs = ofs;
// ofs乘以2再加1
ofs = (ofs << 1) + 1;
// 正整数溢出
if (ofs <= 0)
ofs = maxOfs;
}
// 最大为maxOfs
if (ofs > maxOfs)
ofs = maxOfs; // 重置ofs和lastOfs为相对于base的位置索引
int tmp = lastOfs;
lastOfs = hint - ofs;
ofs = hint - tmp;
}
assert -1 <= lastOfs && lastOfs < ofs && ofs <= len; /*
* Now a[base+lastOfs] < key <= a[base+ofs], so key belongs somewhere
* to the right of lastOfs but no farther right than ofs. Do a binary
* search, with invariant a[base + lastOfs - 1] < key <= a[base + ofs].
*/
lastOfs++;
// 查找准确位置
while (lastOfs < ofs) {
// 中间位置
int m = lastOfs + ((ofs - lastOfs) >>> 1); if (c.compare(key, a[base + m]) > 0)
lastOfs = m + 1; // a[base + m] < key
else
ofs = m; // key <= a[base + m]
}
assert lastOfs == ofs; // so a[base + ofs - 1] < key <= a[base + ofs]
return ofs;
} /**
* Like gallopLeft, except that if the range contains an element equal to
* key, gallopRight returns the index after the rightmost equal element.
*
* @param key the key whose insertion point to search for
* @param a the array in which to search
* @param base the index of the first element in the range
* @param len the length of the range; must be > 0
* @param hint the index at which to begin the search, 0 <= hint < n.
* The closer hint is to the result, the faster this method will run.
* @param c the comparator used to order the range, and to search
* @return the int k, 0 <= k <= n such that a[b + k - 1] <= key < a[b + k]
*/
private static <T> int gallopRight(T key, T[] a, int base, int len,
int hint, Comparator<? super T> c) {
assert len > 0 && hint >= 0 && hint < len; int ofs = 1;
int lastOfs = 0;
if (c.compare(key, a[base + hint]) < 0) {
// 从base + hint位置向前搜索区间[base, base + hint], 使得a[b+hint - ofs] <= key < a[b+hint - lastOfs]
int maxOfs = hint + 1;
while (ofs < maxOfs && c.compare(key, a[base + hint - ofs]) < 0) {
// 记录上次查找位置
lastOfs = ofs;
// 乘以2 加1
ofs = (ofs << 1) + 1;
// 正整数溢出
if (ofs <= 0)
ofs = maxOfs;
}
// 最大为maxOfs
if (ofs > maxOfs)
ofs = maxOfs; // 重置ofs和lastOfs为相对于base的位置索引
int tmp = lastOfs;
lastOfs = hint - ofs;
ofs = hint - tmp;
} else { // a[b + hint] <= key
// 搜索区间[base + hint, base + len -1]使得a[b+hint + lastOfs] <= key < a[b+hint + ofs]
int maxOfs = len - hint;
while (ofs < maxOfs && c.compare(key, a[base + hint + ofs]) >= 0) {
lastOfs = ofs;
ofs = (ofs << 1) + 1;
// 正整数溢出
if (ofs <= 0)
ofs = maxOfs;
}
if (ofs > maxOfs)
ofs = maxOfs; // 重置ofs和lastOfs为相对于base的位置索引
lastOfs += hint;
ofs += hint;
}
assert -1 <= lastOfs && lastOfs < ofs && ofs <= len; lastOfs++;
// 查找key的准确位置
while (lastOfs < ofs) {
int m = lastOfs + ((ofs - lastOfs) >>> 1); if (c.compare(key, a[base + m]) < 0)
ofs = m; // key < a[b + m]
else
lastOfs = m + 1; // a[b + m] <= key
}
// 最终会找到m使得k >= a[base + m], 而此时lastOfs == ofs且lastOfs = m +1, 则ofs = m +1, 因此a[base + ofs] > k >= a[base + ofs -1], ofs即m+1, ofs - 1即为m, 因此ofs位置的值大于key
assert lastOfs == ofs; // so a[b + ofs - 1] <= key < a[b + ofs]
return ofs;
} /**
*基于以上gallopRight方法最后查找key的索引的解释, 因此run1的第一个元素一定大于run2的第一个元素, *而run2中run1最后一个元素所在索引位置之后的值也被忽略掉, 因此run1的最后一个元素大于run2中的所有元素的值。*/ /**
* 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.)
*
* @param base1 index of first element in first run to be merged
* @param len1 length of first run to be merged (must be > 0)
* @param base2 index of first element in second run to be merged
* (must be aBase + aLen)
* @param len2 length of second run to be merged (must be > 0)
*/
private void mergeLo(int base1, int len1, int base2, int len2) {
assert len1 > 0 && len2 > 0 && base1 + len1 == base2; // Copy first run into temp array
T[] a = this.a; // For performance
T[] tmp = ensureCapacity(len1);
int cursor1 = tmpBase; // Indexes into tmp array
int cursor2 = base2; // Indexes int a
// 目标位置从base1的索引开始, 因为base1在base2之前, 下面会将base1的内容放入临时数组, 这样run1中的内容就可以覆盖了
int dest = base1; // Indexes int a
// 把第一个序列的内容放入临时数组tmp中
System.arraycopy(a, base1, tmp, cursor1, len1); // Move first element of second run and deal with degenerate cases
// 因为run1的第一个元素大于run2的第一个元素, 因此将run2的第一个元素先放进
a[dest++] = a[cursor2++];
// 如果run2只有一个元素, 则将run1中剩余的元素放入正确位置后返回
if (--len2 == 0) {
System.arraycopy(tmp, cursor1, a, dest, len1);
return;
}
// 如果run1只有一个元素, 因为run1的最后一个元素大于run2中的所有元素, 因此先将run2中的元素放入正确位置, 然后将run1的唯一的一个元素放入最后一个位置, 然后返回
if (len1 == 1) {
System.arraycopy(a, cursor2, a, dest, len2);
a[dest + len2] = tmp[cursor1]; // Last elt of run 1 to end of merge
return;
} Comparator<? super T> c = this.c; // Use local variable for performance
int minGallop = this.minGallop; // " " " " "
outer:
while (true) {
int count1 = 0; // Number of times in a row that first run won
int count2 = 0; // Number of times in a row that second run won /*
* Do the straightforward thing until (if ever) one run starts
* winning consistently.
*/
do {
assert len1 > 1 && len2 > 0;
if (c.compare(a[cursor2], tmp[cursor1]) < 0) {
a[dest++] = a[cursor2++];
count2++;
count1 = 0;
if (--len2 == 0)
break outer;
} else {
a[dest++] = tmp[cursor1++];
count1++;
count2 = 0;
if (--len1 == 1)
break outer;
}
// 当每个序列中的连续放入目标位置的元素个数小于minGallop时, 这样分别拷贝就可以了
} while ((count1 | count2) < minGallop); /*
* One run is winning so consistently that galloping may be a
* huge win. So try that, and continue galloping until (if ever)
* neither run appears to be winning consistently anymore.
*/
// 因为两个run序列的大小是近似相等的, 如果一个序列连续超过minGallop个数的元素被放入目标位置, 则另一个有接近大小的连续序列等待被放入正确位置,切换成Gallopping模式
do {
assert len1 > 1 && len2 > 0;
//查找run1第一个大于run2中第一个元素的元素的位置索引
count1 = gallopRight(a[cursor2], tmp, cursor1, len1, 0, c);
if (count1 != 0) {
// run1中count1之前的元素全部放入目标序列
System.arraycopy(tmp, cursor1, a, dest, count1);
// 移动索引位置
dest += count1;
cursor1 += count1;
len1 -= count1;
if (len1 <= 1) // len1 == 1 || len1 == 0
break outer;
}
// 移动run2的第一个元素到目标序列中
a[dest++] = a[cursor2++];
// 如果run2中没有其他元素则跳出
if (--len2 == 0)
break outer; // 查找run2中第一个小于等于run1当前元素的元素的位置索引
count2 = gallopLeft(tmp[cursor1], a, cursor2, len2, 0, c);
if (count2 != 0) {
// 拷贝count2之后的元素到目标序列
System.arraycopy(a, cursor2, a, dest, count2);
dest += count2;
cursor2 += count2;
len2 -= count2;
// run2中没有其他元素则跳出
if (len2 == 0)
break outer;
} // 此时run2中的第一个元素大于等于run1中的第一个元素, 拷贝run1中的第一个元素到目标序列
a[dest++] = tmp[cursor1++];
// 如果run1中只有一个元素则跳出
if (--len1 == 1)
break outer;
// 动态调整minGallop的值
minGallop--;
} while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP);
if (minGallop < 0)
minGallop = 0;
// 调整minGallop的值, 使得在有序序列不多的情况下不用Gallopping模式
minGallop += 2; // Penalize for leaving gallop mode
} // End of "outer" loop
this.minGallop = minGallop < 1 ? 1 : minGallop; // Write back to field // run1中只有一个元素
if (len1 == 1) {
assert len2 > 0;
System.arraycopy(a, cursor2, a, dest, len2);
a[dest + len2] = tmp[cursor1]; // Last elt of run 1 to end of merge
} else if (len1 == 0) {
// 因为run1中的最后一个元素大于run2中的所有元素, 因此这种情况不存在
throw new IllegalArgumentException(
"Comparison method violates its general contract!");
} else {
// run2中已经没有元素
assert len2 == 0;
assert len1 > 1;
System.arraycopy(tmp, cursor1, a, dest, len1);
}
} /**
* 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.)
*
* @param base1 index of first element in first run to be merged
* @param len1 length of first run to be merged (must be > 0)
* @param base2 index of first element in second run to be merged
* (must be aBase + aLen)
* @param len2 length of second run to be merged (must be > 0)
*/
private void mergeHi(int base1, int len1, int base2, int len2) {
assert len1 > 0 && len2 > 0 && base1 + len1 == base2; // Copy second run into temp array
T[] a = this.a; // For performance
T[] tmp = ensureCapacity(len2);
int tmpBase = this.tmpBase;
// 将run2中的所有元素放入临时数组tmp中
System.arraycopy(a, base2, tmp, tmpBase, len2); int cursor1 = base1 + len1 - 1; // Indexes into a
int cursor2 = tmpBase + len2 - 1; // Indexes into tmp array
// 从后往前插入元素
int dest = base2 + len2 - 1; // Indexes into a // Move last element of first run and deal with degenerate cases
// run1的最后一个元素导入目标位置
a[dest--] = a[cursor1--];
// 如果run1中只有一个元素, 将run2中的剩余元素放入目标位置(从后往前)
if (--len1 == 0) {
System.arraycopy(tmp, tmpBase, a, dest - (len2 - 1), len2);
return;
}
if (len2 == 1) {
// run2中只有一个元素, 因为run1的第一个元素大于run2的第一个元素, 因此, run2中唯一的一个元素小于run1中所有的元素, 因此将run1中的元素全部放入目标位置, 最后将唯一的run2中的一个元素放入第一个位置
dest -= len1;
cursor1 -= len1;
System.arraycopy(a, cursor1 + 1, a, dest + 1, len1);
a[dest] = tmp[cursor2];
return;
} Comparator<? super T> c = this.c; // Use local variable for performance
int minGallop = this.minGallop; // " " " " "
outer:
while (true) {
int count1 = 0; // Number of times in a row that first run won
int count2 = 0; // Number of times in a row that second run won /*
* Do the straightforward thing until (if ever) one run
* appears to win consistently.
*/
do {
assert len1 > 0 && len2 > 1;
if (c.compare(tmp[cursor2], a[cursor1]) < 0) {
// 从后往前放入目标位置
a[dest--] = a[cursor1--];
count1++;
count2 = 0;
// run1中没有了元素
if (--len1 == 0)
break outer;
} else {
a[dest--] = tmp[cursor2--];
count2++;
count1 = 0;
// run2中只有一个剩余元素
if (--len2 == 1)
break outer;
}
} while ((count1 | count2) < minGallop); /*
* One run is winning so consistently that galloping may be a
* huge win. So try that, and continue galloping until (if ever)
* neither run appears to be winning consistently anymore.
*/
do {
assert len1 > 0 && len2 > 1;
// 找到大于run2当前位置的元素的run1中元素, 因为是从后往前查找, 因此找到的位置比如k1,k1之后的所有元素大于run1, run2中的剩余所有元素
count1 = len1 - gallopRight(tmp[cursor2], a, base1, len1, len1 - 1, c);
if (count1 != 0) {
dest -= count1;
cursor1 -= count1;
len1 -= count1;
// 拷贝run1中的大的元素
System.arraycopy(a, cursor1 + 1, a, dest + 1, count1);
// run1中没有元素, 跳出
if (len1 == 0)
break outer;
}
// run2的当前元素拷贝到目标位置
a[dest--] = tmp[cursor2--];
if (--len2 == 1)
break outer; // 找到run2中大于等于run1当前元素的元素的位置索引比如K2, 则k2之后的所有元素大于run1, run2中的剩余元素
count2 = len2 - gallopLeft(a[cursor1], tmp, tmpBase, len2, len2 - 1, c);
if (count2 != 0) {
dest -= count2;
cursor2 -= count2;
len2 -= count2;
System.arraycopy(tmp, cursor2 + 1, a, dest + 1, count2);
if (len2 <= 1) // len2 == 1 || len2 == 0
break outer;
}
// 拷贝run1的当前元素到目标位置, 因为a[cursior1]大于等于run2中的剩余元素
a[dest--] = a[cursor1--];
if (--len1 == 0)
break outer;
minGallop--;
} while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP);
if (minGallop < 0)
minGallop = 0;
minGallop += 2; // Penalize for leaving gallop mode
} // End of "outer" loop
this.minGallop = minGallop < 1 ? 1 : minGallop; // Write back to field // run2中只有一个元素
if (len2 == 1) {
assert len1 > 0;
dest -= len1;
cursor1 -= len1;
// 拷贝run1中的所有元素到目标位置
System.arraycopy(a, cursor1 + 1, a, dest + 1, len1);
// run2的最后一个元素放入第一个位置
a[dest] = tmp[cursor2]; // Move first elt of run2 to front of merge
} else if (len2 == 0) {
// 因为run2的第一个元素小于run1, run2中的所有元素, 因此run2不可能最后为空
throw new IllegalArgumentException(
"Comparison method violates its general contract!");
} else {
assert len1 == 0;
assert len2 > 0;
// 拷贝run2中的剩余元素
System.arraycopy(tmp, tmpBase, a, dest - (len2 - 1), len2);
}
} /**
* 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.
*
* @param minCapacity the minimum required capacity of the tmp array
* @return tmp, whether or not it grew
*/
private T[] ensureCapacity(int minCapacity) {
if (tmpLen < minCapacity) {
// Compute smallest power of 2 > minCapacity
int newSize = minCapacity;
newSize |= newSize >> 1;
newSize |= newSize >> 2;
newSize |= newSize >> 4;
newSize |= newSize >> 8;
newSize |= newSize >> 16;
newSize++; if (newSize < 0) // Not bloody likely!
newSize = minCapacity;
else
newSize = Math.min(newSize, a.length >>> 1); @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
T[] newArray = (T[])java.lang.reflect.Array.newInstance
(a.getClass().getComponentType(), newSize);
tmp = newArray;
tmpLen = newSize;
tmpBase = 0;
}
return tmp;
}
}

TimSort Java源码个人解读的更多相关文章

  1. 【java源码】解读HashTable类背后的实现细节

    HashTable这个类实现了哈希表从key映射到value的数据结构形式.任何非null的对象都可以作为key或者value. 要在hashtable中存储和检索对象,作为key的对象必须实现has ...

  2. 【源码阅读】Java集合之二 - LinkedList源码深度解读

    Java 源码阅读的第一步是Collection框架源码,这也是面试基础中的基础: 针对Collection的源码阅读写一个系列的文章; 本文是第二篇LinkedList. ---@pdai JDK版 ...

  3. Java源码解读(一)——HashMap

    HashMap作为常用的一种数据结构,阅读源码去了解其底层的实现是十分有必要的.在这里也分享自己阅读源码遇到的困难以及自己的思考. HashMap的源码介绍已经有许许多多的博客,这里只记录了一些我看源 ...

  4. 【源码阅读】Java集合之三 - ArrayDeque源码深度解读

    Java 源码阅读的第一步是Collection框架源码,这也是面试基础中的基础: 针对Collection的源码阅读写一个系列的文章,本文是第三篇ArrayDeque. ---@pdai JDK版本 ...

  5. 【源码阅读】Java集合之一 - ArrayList源码深度解读

    Java 源码阅读的第一步是Collection框架源码,这也是面试基础中的基础: 针对Collection的源码阅读写一个系列的文章,从ArrayList开始第一篇. ---@pdai JDK版本 ...

  6. 【java集合框架源码剖析系列】java源码剖析之java集合中的折半插入排序算法

    注:关于排序算法,博主写过[数据结构排序算法系列]数据结构八大排序算法,基本上把所有的排序算法都详细的讲解过,而之所以单独将java集合中的排序算法拿出来讲解,是因为在阿里巴巴内推面试的时候面试官问过 ...

  7. 如何阅读Java源码 阅读java的真实体会

    刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心.   说到技术基础,我打个比 ...

  8. Android反编译(一)之反编译JAVA源码

    Android反编译(一) 之反编译JAVA源码 [目录] 1.工具 2.反编译步骤 3.实例 4.装X技巧 1.工具 1).dex反编译JAR工具  dex2jar   http://code.go ...

  9. 如何阅读Java源码

    刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动.源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 说到技术基础,我打个比方吧, ...

随机推荐

  1. 指针*和取地址&函数输入使用

    函数输入问题: 1 带&和不带& (参数本身还是拷贝一份参数) 2 函数输入指针 #include <iostream> using namespace std; int ...

  2. 10-网页,网站,微信公众号基础入门(使用微信自带配置选项实现Airkiss配网)

    https://www.cnblogs.com/yangfengwu/p/11066036.html 如果提交失败多提交两次,只要上一节可以,,这一节一定可以的 如果没有设备 这个是我的二维码 咱就测 ...

  3. [RN] React Native 删除第三方开源组件依赖包 后 还要做的 (以 删除 react-native-video为例)

    近期测试使用了下  react-native-video 使用一直不成功,后来想着删除掉, 使用命令: npm uninstall react-native-video 重新编译后,还是一直报错 后来 ...

  4. mysql初始

    数据(data) : -描述事物的符号记录称为数据,符号既可以是数据,文字,图片,声音,语言等,符号都可以经过数字化后存入计算机中 - 计算机中描述一个事物,就需要抽取这一事物的典型特征,组成一条记录 ...

  5. java的static和this

    1>static:静态修饰符   static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念.    被sta ...

  6. .htaccess tricks总结

    目录 .htaccess tricks总结 一.什么是.htaccess 二.利用条件 三.利用方式 && tricks 1.将指定后缀名的文件当做php解析 2.php_value利 ...

  7. Net core学习系列(九)——Net Core配置

    一.简介 NET Core为我们提供了一套用于配置的API,它为程序提供了运行时从文件.命令行参数.环境变量等读取配置的方法.配置都是键值对的形式,并且支持嵌套,.NET Core还内建了从配置反序列 ...

  8. 论文阅读 | Recurrent Attentional Reinforcement Learning for Multi-label Image Recognition

    源地址 arXiv:1712.07465: Recurrent Attentional Reinforcement Learning for Multi-label Image Recognition ...

  9. pyzbar 安装

    什么是ZBar? ZBar是一个开源库,用于扫描.读取二维码和条形码.支持的二维码包括:EAN/UPC,QR等. 1.windows 下直接pip 安装: pip install pyzbar 2.u ...

  10. web项目脱敏白名单管理

    写在前面 下午没事, 看一下同事写的脱敏白名单管理功能. 所谓的脱敏就是将页面中查询出的信息列表当中的身份证号及手机号等关键信息部分用*号代替.该功能主要思路是新增一个页面用于配置哪些页面以及页面中哪 ...