Created
May 1, 2017 08:05
-
-
Save Hanaasagi/b4fc5c1f5e162db052ff92395db9a5c8 to your computer and use it in GitHub Desktop.
Timsort 算法实现 JDK1.7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class TimSortTest { | |
public static void main(String[] args) { | |
int arr[] = {3, 6, 8, 9, 15, 13, 11, 7, 42, 58, 100, 22, 26, 39, 38, 43, 50, | |
70, 46, 10, 36, 56, 58, 56, 59, 10, 71, 89,46, 10, 36, 56, 58, 56, 59, 10, 71, 89}; | |
System.out.println("before sort: " + Arrays.toString(arr)); | |
System.out.println("array length: " + arr.length); | |
TimSort.sort(arr, 0, arr.length, null, 0, 0); | |
System.out.println("after sort: " + Arrays.toString(arr)); | |
} | |
} | |
class TimSort { | |
private static final int MIN_MERGE = 32; | |
/** | |
* The array being sorted. | |
*/ | |
private final int[] a; | |
/** | |
* The comparator for this sort. | |
*/ | |
/** | |
* 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 int[] 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 | |
*/ | |
public TimSort(int[] a, int[] work, int workBase, int workLen) { | |
this.a = a; | |
// Allocate temp storage (which may be increased later if necessary) | |
int len = a.length; | |
int tlen = (len < 2 * INITIAL_TMP_STORAGE_LENGTH) ? | |
len >>> 1 : INITIAL_TMP_STORAGE_LENGTH; | |
if (work == null || workLen < tlen || workBase + tlen > work.length) { | |
int[] newArray = (int[])java.lang.reflect.Array.newInstance | |
(a.getClass().getComponentType(), tlen); | |
tmp = newArray; | |
tmpBase = 0; | |
tmpLen = tlen; | |
} | |
else { | |
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. | |
*/ | |
int stackLen = (len < 120 ? 5 : | |
len < 1542 ? 10 : | |
len < 119151 ? 24 : 40); | |
runBase = new int[stackLen]; | |
runLen = new int[stackLen]; | |
} | |
public static void sort(int[] a, int lo, int hi, | |
int[] work, int workBase, int workLen) { | |
assert a != null && lo >= 0 && lo <= hi && hi <= a.length; | |
// hi : high | |
// lo : lower | |
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 | |
// mini-TimSort 会从顺序的遍历数组中找出第一个 Run | |
// 一定会存在一个 Run | |
// [2, 2, 3, 4] => Run [2] | |
// [2, 3, 1] => Run [2, 3] | |
// [3, 2, 1] => Run [3, 2, 1] | |
// 然后直接对于 | |
if (nRemaining < MIN_MERGE) { // MIN_MERGE = 32 | |
int initRunLen = countRunAndMakeAscending(a, lo, hi); | |
// System.out.println("initRunLen: " + initRunLen); | |
binarySort(a, lo, hi, lo + initRunLen); | |
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 ts = new TimSort(a, work, workBase, workLen); | |
int minRun = minRunLength(nRemaining); | |
do { | |
// Identify next run | |
int runLen = countRunAndMakeAscending(a, lo, hi); | |
// If run is short, extend to min(minRun, nRemaining) | |
if (runLen < minRun) { | |
int force = nRemaining <= minRun ? nRemaining : minRun; | |
binarySort(a, lo, lo + force, lo + runLen); | |
runLen = force; | |
} | |
// Push run onto pending-run stack, and maybe merge | |
// 入栈 | |
ts.pushRun(lo, runLen); | |
// 进行归并 | |
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 | |
*/ | |
// 当 run 的长度小于 minRun 时被调用 | |
@SuppressWarnings("fallthrough") | |
private static void binarySort(int[] a, int lo, int hi, int start | |
) { | |
// lo 为 run 的起始位置 | |
// hi 为 run 应该结束的位置 (即 run 的起始位置 + minRun) | |
// start 当前有序部分的结束位置 (start 后的元素需要插入至 run 中) | |
//System.out.println(lo + "到" +hi); | |
assert lo <= start && start <= hi; | |
if (start == lo) | |
start++; | |
for ( ; start < hi; start++) { | |
int pivot = a[start]; | |
// Set left (and right) to the index where a[start] (pivot) belongs | |
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 (pivot < a[mid]) | |
right = mid; | |
else | |
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 | |
// 将位置后的元素向后移动一个位置 | |
// 在元素和要插入的位置很近时,避免使用 arraycopy | |
switch (n) { | |
case 2: a[left + 2] = a[left + 1]; | |
case 1: a[left + 1] = a[left]; | |
break; | |
default: System.arraycopy(a, left, a, left + 1, n); | |
} | |
// 插入元素 | |
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 int countRunAndMakeAscending(int[] a, int lo, int hi | |
) { | |
assert lo < hi; | |
int runHi = lo + 1; | |
if (runHi == hi) | |
return 1; | |
// Find end of run, and reverse range if descending | |
// 根据前两个元素的比较,判断具有升序趋势还是降序趋势 | |
if (a[runHi++]<a[lo]) { // Descending | |
while (runHi < hi && a[runHi]<a[runHi - 1]) | |
runHi++; | |
// Descending to Ascending | |
reverseRange(a, lo, runHi); | |
} else { // Ascending | |
while (runHi < hi && a[runHi]>=a[runHi - 1]) | |
runHi++; | |
} | |
System.out.println("Run length: " + (runHi-lo)); | |
return runHi - lo; | |
} | |
/** | |
* Reverse the specified range of the specified array. | |
* | |
* @param a2 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(int[] a2, int lo, int hi) { | |
hi--; | |
while (lo < hi) { | |
Object t = a2[lo]; | |
a2[lo++] = a2[hi]; | |
a2[hi--] = (int) 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) { | |
// n 为数组长度 | |
assert n >= 0; | |
int r = 0; // Becomes 1 if any 1 bits are shifted off | |
while (n >= MIN_MERGE) { // MIN_MERGE = 32 Python 中为 64 | |
r |= (n & 1); | |
n >>= 1; | |
} | |
System.out.println("minRunLength: " + (n+r)); | |
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: | |
* 反复 merge 直到这两个条件满足 i 为 stack 的长度 | |
* 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]) { | |
// [n-1] [n] [n+1] | |
// run[n] 和更小的那个进行 merge | |
if (runLen[n - 1] < runLen[n + 1]) | |
n--; | |
// 对 run[n] 和 run[n+1] 进行 merge | |
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; | |
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; | |
// run1 的起始位置 | |
int base1 = runBase[i]; | |
int len1 = runLen[i]; | |
// run2 的起始位置 | |
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] 的长度为合并后的长度 | |
runLen[i] = len1 + len2; | |
// 若合并的是相对于栈顶 2rd 和 3rd 的 run 需要将栈顶向下移动一个单位 | |
// | Z | <- top | |
// | Y | | |
// | X | <- bottom | |
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); | |
assert k >= 0; | |
// base1 至 base1 + k 的元素为两个 run 公共最小,不需要参与排序 | |
base1 += k; | |
len1 -= k; | |
// run1 即为公共最小,不需要再进行排序 | |
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 的位置,这样可以筛选出公共最大的部分 | |
// base2 + len2 后的元素为两个 run 公共最大,不需要参与排序 | |
len2 = gallopLeft(a[base1 + len1 - 1], a, base2, len2, len2 - 1); | |
assert len2 >= 0; | |
// run2 即为公共最大,不需要再进行排序 | |
if (len2 == 0) | |
return; | |
// 至于为什么是在 run1 中找 run2 最小元素的位置;在 run2 中找 run1 最大元素的位置 | |
// 是因为 run1 run2 是线性排列的 | |
// (1, 3, { 5, 7,) (4, 5, 6, } 10, 12) | |
// () 中为两个 run | |
// {} 中为需要排序的部分 | |
// 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 base 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 int gallopLeft(int key, int[] a, int base, int len, int hint | |
) { | |
// key 为准备插入的元素 | |
// hint 为开始查找的偏移 | |
assert len > 0 && hint >= 0 && hint < len; | |
int lastOfs = 0; | |
int ofs = 1; | |
// key 比 a[base + hint] 小,倍增 ofs = 1, 3, 7, 2^n-1 使得 | |
// key > a[base + hist - ofs ] | |
// key 比 a[base + hint] 大,倍增 ofs = 1, 3, 7, 2^n-1 使得 | |
// key < a[base + hist + ofs ] | |
if (key > a[base + hint]) { | |
// Gallop right until a[base+hint+lastOfs] < key <= a[base+hint+ofs] | |
int maxOfs = len - hint; | |
while (ofs < maxOfs && key > a[base + hint + ofs]) { | |
lastOfs = ofs; | |
// 1, 3, 7, 2^n-1 | |
ofs = (ofs << 1) + 1; | |
if (ofs <= 0) // int overflow | |
ofs = maxOfs; | |
} | |
if (ofs > maxOfs) | |
ofs = maxOfs; | |
// Make offsets relative to base | |
lastOfs += hint; | |
ofs += hint; | |
} else { // key <= a[base + hint] | |
// Gallop left until a[base+hint-ofs] < key <= a[base+hint-lastOfs] | |
final int maxOfs = hint + 1; | |
while (ofs < maxOfs && key <= a[base + hint - ofs]) { | |
lastOfs = ofs; | |
ofs = (ofs << 1) + 1; | |
if (ofs <= 0) // int overflow | |
ofs = maxOfs; | |
} | |
if (ofs > maxOfs) | |
ofs = maxOfs; | |
// Make offsets relative to 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 = base + 2^(n-1)-1 | |
// ofs = 2^n-1 | |
// a[base+lastOfs] < key <= a[base+ofs], 在 base+lastOfs-1到 base+ofs 范围内执行二分查找 | |
// 确认 key 应当插入的位置 | |
lastOfs++; | |
while (lastOfs < ofs) { | |
int m = lastOfs + ((ofs - lastOfs) >>> 1); | |
if (key>a[base + m]) | |
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 int gallopRight(int key, int[] a, int base, int len, | |
int hint) { | |
assert len > 0 && hint >= 0 && hint < len; | |
int ofs = 1; | |
int lastOfs = 0; | |
if (key < a[base + hint]) { | |
// Gallop left until a[b+hint - ofs] <= key < a[b+hint - lastOfs] | |
int maxOfs = hint + 1; | |
while (ofs < maxOfs && key < a[base + hint - ofs]) { | |
lastOfs = ofs; | |
ofs = (ofs << 1) + 1; | |
if (ofs <= 0) // int overflow | |
ofs = maxOfs; | |
} | |
if (ofs > maxOfs) | |
ofs = maxOfs; | |
// Make offsets relative to b | |
int tmp = lastOfs; | |
lastOfs = hint - ofs; | |
ofs = hint - tmp; | |
} else { // a[b + hint] <= key | |
// Gallop right until a[b+hint + lastOfs] <= key < a[b+hint + ofs] | |
int maxOfs = len - hint; | |
while (ofs < maxOfs && key>=a[base + hint + ofs]) { | |
lastOfs = ofs; | |
ofs = (ofs << 1) + 1; | |
if (ofs <= 0) // int overflow | |
ofs = maxOfs; | |
} | |
if (ofs > maxOfs) | |
ofs = maxOfs; | |
// Make offsets relative to b | |
lastOfs += hint; | |
ofs += hint; | |
} | |
assert -1 <= lastOfs && lastOfs < ofs && ofs <= len; | |
/* | |
* Now a[b + lastOfs] <= key < a[b + ofs], so key belongs somewhere to | |
* the right of lastOfs but no farther right than ofs. Do a binary | |
* search, with invariant a[b + lastOfs - 1] <= key < a[b + ofs]. | |
*/ | |
lastOfs++; | |
while (lastOfs < ofs) { | |
int m = lastOfs + ((ofs - lastOfs) >>> 1); | |
if (key<a[base + m]) | |
ofs = m; // key < a[b + m] | |
else | |
lastOfs = m + 1; // a[b + m] <= key | |
} | |
assert lastOfs == ofs; // so a[b + ofs - 1] <= key < a[b + ofs] | |
return ofs; | |
} | |
/** | |
* 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) | |
*/ | |
// 对待排区间进行 merge | |
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 | |
int[] a = this.a; // For performance | |
// 申请临时数组空间,并将 run1 复制进去 | |
int[] tmp = ensureCapacity(len1); | |
int cursor1 = tmpBase; // Indexes into tmp array | |
int cursor2 = base2; // Indexes int a | |
int dest = base1; // Indexes int a | |
System.arraycopy(a, base1, tmp, cursor1, len1); | |
// Move first element of second run and deal with degenerate cases | |
a[dest++] = a[cursor2++]; | |
// 若 run2 只有一个元素,将临时数组中的元素拷贝到后面即可 | |
if (--len2 == 0) { | |
System.arraycopy(tmp, cursor1, a, dest, len1); | |
return; | |
} | |
// 若 run1 只有一个元素,将 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; | |
} | |
// Use local variable for performance | |
// minGallop = 7 | |
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. | |
*/ | |
// 对 run1 和 run2 进行 merge | |
do { | |
assert len1 > 1 && len2 > 0; | |
if (a[cursor2] < tmp[cursor1]) { | |
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; | |
} | |
// WTF 这个相当于 count1 < minGallop && count2 < minGallop | |
// 因为 count1 或 count2 总有一个为 0 | |
// 如果在这里跳出说明遇到了某一个 run 中连续存在比另一个 run 的某个元素大的情况 | |
} 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. | |
*/ | |
// 再次利用 gallop 缩小范围 | |
do { | |
assert len1 > 1 && len2 > 0; | |
count1 = gallopRight(a[cursor2], tmp, cursor1, len1, 0); | |
if (count1 != 0) { | |
System.arraycopy(tmp, cursor1, a, dest, count1); | |
dest += count1; | |
cursor1 += count1; | |
len1 -= count1; | |
if (len1 <= 1) // len1 == 1 || len1 == 0 | |
break outer; | |
} | |
a[dest++] = a[cursor2++]; | |
if (--len2 == 0) | |
break outer; | |
count2 = gallopLeft(tmp[cursor1], a, cursor2, len2, 0); | |
if (count2 != 0) { | |
System.arraycopy(a, cursor2, a, dest, count2); | |
dest += count2; | |
cursor2 += count2; | |
len2 -= count2; | |
if (len2 == 0) | |
break outer; | |
} | |
a[dest++] = tmp[cursor1++]; | |
if (--len1 == 1) | |
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 | |
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) { | |
throw new IllegalArgumentException( | |
"Comparison method violates its general contract!"); | |
} else { | |
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 | |
int[] a = this.a; // For performance | |
int[] tmp = ensureCapacity(len2); | |
int tmpBase = this.tmpBase; | |
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 | |
a[dest--] = a[cursor1--]; | |
if (--len1 == 0) { | |
System.arraycopy(tmp, tmpBase, a, dest - (len2 - 1), len2); | |
return; | |
} | |
if (len2 == 1) { | |
dest -= len1; | |
cursor1 -= len1; | |
System.arraycopy(a, cursor1 + 1, a, dest + 1, len1); | |
a[dest] = tmp[cursor2]; | |
return; | |
} | |
// 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 (tmp[cursor2]<a[cursor1]) { | |
a[dest--] = a[cursor1--]; | |
count1++; | |
count2 = 0; | |
if (--len1 == 0) | |
break outer; | |
} else { | |
a[dest--] = tmp[cursor2--]; | |
count2++; | |
count1 = 0; | |
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; | |
count1 = len1 - gallopRight(tmp[cursor2], a, base1, len1, len1 - 1); | |
if (count1 != 0) { | |
dest -= count1; | |
cursor1 -= count1; | |
len1 -= count1; | |
System.arraycopy(a, cursor1 + 1, a, dest + 1, count1); | |
if (len1 == 0) | |
break outer; | |
} | |
a[dest--] = tmp[cursor2--]; | |
if (--len2 == 1) | |
break outer; | |
count2 = len2 - gallopLeft(a[cursor1], tmp, tmpBase, len2, len2 - 1); | |
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; | |
} | |
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 | |
if (len2 == 1) { | |
assert len1 > 0; | |
dest -= len1; | |
cursor1 -= len1; | |
System.arraycopy(a, cursor1 + 1, a, dest + 1, len1); | |
a[dest] = tmp[cursor2]; // Move first elt of run2 to front of merge | |
} else if (len2 == 0) { | |
throw new IllegalArgumentException( | |
"Comparison method violates its general contract!"); | |
} else { | |
assert len1 == 0; | |
assert len2 > 0; | |
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 int[] 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"}) | |
int[] newArray = (int[])java.lang.reflect.Array.newInstance | |
(a.getClass().getComponentType(), newSize); | |
tmp = newArray; | |
tmpLen = newSize; | |
tmpBase = 0; | |
} | |
return tmp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment