DualPivotQuickSort匯集了多種排序算法,稱之為DualPivotQuickSort並不合適。不同的排序算法有不同的使用場景。看懂此文件,排序算法就算徹底搞懂了。
本文只介紹有用的代碼片段,DualPivotQuickSort.java可以用這些代碼片段拼湊起來。
本文中的排序對數組a的[left,right]閉區間進行排序。
常量
- QUICKSORT_THRESHOLD = 286
小於此值使用快排,大於此值使用歸並排序。 - INSERTION_SORT_THRESHOLD = 47
小於此值使用插入排序,大於此值使用快速排序。 - COUNTING_SORT_THRESHOLD_FOR_BYTE = 29, COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR = 3200
byte數組排序時,如果元素數量較多,那么使用counting排序,即使用一個包含256個元素的桶進行排序。
short數組排序時,如果元素數量較多,那么使用counting排序,即使用65536的桶進行排序。 - NUM_SHORT_VALUES、NUM_BYTE_VALUES、NUM_CHAR_VALUES
分別表示short、byte、char類型的數據的種數,用於counting排序。 - MAX_RUN_COUNT = 67
歸並排序中run的個數。
常量中除了QUICKSORT_THRESHOLD其余都是用來選擇排序算法的,選擇排序算法主要考慮元素個數。
- 當元素個數遠遠大於元素種數,使用counting排序
- 當元素個數較多且基本有序(遞增片段較少),使用歸並排序
- 當元素個數較多且較為無序,使用快速排序
- 當元素個數較少,使用插入排序
普通的插入排序
for (int i = left, j = i; i < right; j = ++i) {
long ai = a[i + 1];
while (ai < a[j]) {
a[j + 1] = a[j];
if (j-- == left) {
break;
}
}
a[j + 1] = ai;
}
改進插入排序:成對插入排序
成對插入排序是對插入排序的改進,每次將兩個元素一起往前移動。
它需要進行一下預處理:跳過第一個有序片段,這個片段的長度一定大於等於1。
這個預處理的優勢在於:可以避免左邊的邊界檢測。在“普通插入排序”部分的代碼中,需要進行邊界檢測。
do {
if (left >= right) {
return;
}
} while (a[++left] >= a[left - 1]);
成對插入排序過程中,left表示第二個元素,k表示第一個元素。
for (int k = left; ++left <= right; k = ++left) {
long a1 = a[k], a2 = a[left];
if (a1 < a2) {//先讓這兩個待插入的元素排好序
a2 = a1; a1 = a[left];
}
while (a1 < a[--k]) {//先讓較大的元素往前走
a[k + 2] = a[k];
}
a[++k + 1] = a1;
while (a2 < a[--k]) {//再讓較小的元素往前走
a[k + 1] = a[k];
}
a[k + 1] = a2;
}
long last = a[right];//因為是成對排序,最后一個元素有可能落單
while (last < a[--right]) {
a[right + 1] = a[right];
}
a[right + 1] = last;
}
普通的快排:單軸快排
單軸快排就是傳統的快速排序,只選擇一個pivot把數組分成左右兩部分。快排中最重要的就是pivot的選取,它直接決定了排序算法的性能。
一般人寫快排時,pivot取第一個元素的取值,或者先隨機一個下標,將此下標對應的元素與第一個元素交換作為pivot。
DualPivotQuickSort中的單軸快排pivot的選取方式是這樣的:首先從[left,right]區間找到5個點,對這五個點的值進行插入排序;然后選取e3作為pivot執行快排。
int seventh = (length >> 3) + (length >> 6) + 1;//length的近似七分之一,這種寫法太炫酷
int e3 = (left + right) >>> 1; // The midpoint
int e2 = e3 - seventh;
int e1 = e2 - seventh;
int e4 = e3 + seventh;
int e5 = e4 + seventh;
對這五個元素進行插入排序時,直接使用if-else實現插入排序。
// Sort these elements using insertion sort
if (a[e2] < a[e1]) { long t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
if (a[e3] < a[e2]) { long t = a[e3]; a[e3] = a[e2]; a[e2] = t;
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
}
if (a[e4] < a[e3]) { long t = a[e4]; a[e4] = a[e3]; a[e3] = t;
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
}
}
if (a[e5] < a[e4]) { long t = a[e5]; a[e5] = a[e4]; a[e4] = t;
if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
}
}
}
單軸快排代碼:left、right表示待排序的區間,less、great表示左右兩個指針,開始時分別等於left和right。這份快排代碼就是傳統的雙指針快排,它有許多種寫法。
for (int k = less; k <= great; ++k) {
if (a[k] == pivot) {
continue;
}
long ak = a[k];
if (ak < pivot) { // Move a[k] to left part
a[k] = a[less];
a[less] = ak;
++less;
} else { // a[k] > pivot - Move a[k] to right part
while (a[great] > pivot) {
--great;
}
if (a[great] < pivot) { // a[great] <= pivot
a[k] = a[less];
a[less] = a[great];
++less;
} else { // a[great] == pivot
/*
* Even though a[great] equals to pivot, the
* assignment a[k] = pivot may be incorrect,
* if a[great] and pivot are floating-point
* zeros of different signs. Therefore in float
* and double sorting methods we have to use
* more accurate assignment a[k] = a[great].
*/
a[k] = pivot;
}
a[great] = ak;
--great;
}
}
sort(a, left, less - 1, leftmost);
sort(a, great + 1, right, false);
快速排序中,注意浮點數的相等並非完全相等,在寫快排時這是一個容易忽略的點。如果直接使用pivot值覆蓋某個數字,可能造成排序后的數組中的值發生變化。
改進的快排:雙軸快排
雙軸快排就是使用兩個pivot划分數組,把數組分為(負無窮,pivot1)、[pivot1,pivot2]、(pivot2,正無窮)三部分。pivot1和pivot2取a[e2]和a[e4]。
//選擇兩個pivot
int pivot1 = a[e2];
int pivot2 = a[e4];
//把left和right放在e2、e4處,讓它們參與排序過程,因為只有[left+1,right-1]區間上的數字才參與排序
a[e2] = a[left];
a[e4] = a[right];
//先貪心地快速移動一波
while (a[++less] < pivot1);
while (a[--great] > pivot2);
//利用雙軸把數組分成三部分,和快排相似
outer:
for (int k = less - 1; ++k <= great; ) {
int ak = a[k];
if (ak < pivot1) { // Move a[k] to left part
a[k] = a[less];
a[less] = ak;
++less;
} else if (ak > pivot2) { // Move a[k] to right part
while (a[great] > pivot2) {
if (great-- == k) {
break outer;
}
}
if (a[great] < pivot1) { // a[great] <= pivot2
a[k] = a[less];
a[less] = a[great];
++less;
} else { // pivot1 <= a[great] <= pivot2
a[k] = a[great];
}
a[great] = ak;
--great;
}
}
// 讓開頭和結尾的pivot1和pivot2回歸到中間來
a[left] = a[less - 1]; a[less - 1] = pivot1;
a[right] = a[great + 1]; a[great + 1] = pivot2;
// Sort left and right parts recursively, excluding known pivots
sort(a, left, less - 2, leftmost);
sort(a, great + 2, right, false);
sort(a, less, great, false);
在以上代碼中,數組被分成了三個區,對三個區分別遞歸調用排序。
其中,在排序中間部分sort(a, less, great, false)
時,有一個技巧:把[less,great]區間划分成(嚴格等於pivot1的區間)、(pivot1和pivot2之間的值)、(嚴格等於pivot2的區間)。
//老規矩,快速走一波
while (a[less] == pivot1) ++less;
while (a[great] == pivot2) --great;
//又是一個雙軸划分過程
outer:
for (int k = less - 1; ++k <= great; ) {
int ak = a[k];
if (ak == pivot1) { // Move a[k] to left part
a[k] = a[less];
a[less] = ak;
++less;
} else if (ak == pivot2) { // Move a[k] to right part
while (a[great] == pivot2) {
if (great-- == k) {
break outer;
}
}
if (a[great] == pivot1) { // a[great] < pivot2
a[k] = a[less];
a[less] = pivot1;
++less;
} else { // pivot1 < a[great] < pivot2
a[k] = a[great];
}
a[great] = ak;
--great;
}
}
經過這個處理,就能夠使得[less,great]區間盡量小,然后再對(pivot1,pivot2)之間的數字進行排序。
為什么雙軸快排比普通快排快?
理論上,分析排序算法的性能主要看元素比較次數。雙軸快排不如普通快排比較次數少。
但是,元素比較次數實際上並不能真實反映排序算法的性能。理論跟實際情況不符合的時候,如果實際情況沒有錯,那么就是理論錯了。
據統計在過去的25年里面,CPU的速度平均每年增長46%, 而內存的帶寬每年只增長37%,那么經過25年的這種不均衡發展,它們之間的差距已經蠻大了。假如這種不均衡持續持續發展,有一天CPU速度再增長也不會讓程序變得更快,因為CPU始終在等待內存傳輸數據,這就是傳說中內存牆(Memory Wall)。排序過程的瓶頸在於內存而不在於CPU,這就像木桶理論:木桶的容量是由最短的那塊板決定的。25年前Dual-Pivot快排可能真的比經典快排要慢,但是25年之后雖然算法還是以前的那個算法,但是計算機已經不是以前的計算機了。在現在的計算機里面Dual-Pivot算法更快!
那么既然光比較元素比較次數這種計算排序算法復雜度的方法已經無法客觀的反映算法優劣了,那么應該如何來評價一個算法呢?作者提出了一個叫做掃描元素個數的算法。
在這種新的算法里面,我們把對於數組里面一個元素的訪問: array[i] 稱為一次掃描。但是對於同一個下標,並且對應的值也不變得話,即使訪問多次我們也只算一次。而且我們不管這個訪問到底是讀還是寫。
其實這個所謂的掃描元素個數反應的是CPU與內存之間的數據流量的大小。
因為內存比較慢,統計CPU與內存之間的數據流量的大小也就把這個比較慢的內存的因素考慮進去了,因此也就比元素比較次數更能體現算法在當下計算機里面的性能指標。
改進的歸並排序:TimSort
把數組划分為若干個遞增片段
以下代碼把數組划分成若干個遞增片段,如果遇到遞減片段會嘗試翻轉數組使之遞增。
如果遞增片段太多(超過MAX_RUN_COUNT),說明數組太亂了,利用歸並排序效果不夠好,這時應該使用快速排序。
int[] run = new int[MAX_RUN_COUNT + 1];
int count = 0; run[0] = left;
// Check if the array is nearly sorted
for (int k = left; k < right; run[count] = k) {
// Equal items in the beginning of the sequence
while (k < right && a[k] == a[k + 1])
k++;
if (k == right) break; // Sequence finishes with equal items
if (a[k] < a[k + 1]) { // ascending
while (++k <= right && a[k - 1] <= a[k]);
} else if (a[k] > a[k + 1]) { // descending
while (++k <= right && a[k - 1] >= a[k]);
// Transform into an ascending sequence
for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
int t = a[lo]; a[lo] = a[hi]; a[hi] = t;
}
}
// Merge a transformed descending sequence followed by an
// ascending sequence
if (run[count] > left && a[run[count]] >= a[run[count] - 1]) {
count--;
}
/*
* The array is not highly structured,
* use Quicksort instead of merge sort.
*/
if (++count == MAX_RUN_COUNT) {
sort(a, left, right, true);
return;
}
}
確定了遞增片段之后,如果發現只有一個遞增片段,那么結果已經是有序的了,直接返回。
if (count == 0) {
// A single equal run
return;
} else if (count == 1 && run[count] > right) {
// Either a single ascending or a transformed descending run.
// Always check that a final run is a proper terminator, otherwise
// we have an unterminated trailing run, to handle downstream.
return;
}
right++;
if (run[count] < right) {
// Corner case: the final run is not a terminator. This may happen
// if a final run is an equals run, or there is a single-element run
// at the end. Fix up by adding a proper terminator at the end.
// Note that we terminate with (right + 1), incremented earlier.
run[++count] = right;
}
非遞歸方式實現歸並排序
歸並排序空間復雜度為O(n),n為元素個數。此函數簽名為static void sort(int[] a, int left, int right,int[] work, int workBase, int workLen)
,表示對數組a在[left,right]區間上排序,排序過程中可用的額外空間為work中的[workBase,workBase+workLen]。如果work給定的空間不夠用,就會新開辟足夠的空間。
// Use or create temporary array b for merging
int[] b; // temp array; alternates with a
int ao, bo; // array offsets from 'left'
int blen = right - left; // space needed for b
if (work == null || workLen < blen || workBase + blen > work.length) {
work = new int[blen];
workBase = 0;
}
if (odd == 0) {
System.arraycopy(a, left, work, workBase, blen);
b = a;
bo = 0;
a = work;
ao = workBase - left;
} else {
b = work;
ao = 0;
bo = workBase - left;
}
// Merging
for (int last; count > 1; count = last) {
for (int k = (last = 0) + 2; k <= count; k += 2) {
int hi = run[k], mi = run[k - 1];
for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
b[i + bo] = a[p++ + ao];
} else {
b[i + bo] = a[q++ + ao];
}
}
run[++last] = hi;
}
if ((count & 1) != 0) {
for (int i = right, lo = run[count - 1]; --i >= lo;
b[i + bo] = a[i + ao]
);
run[++last] = right;
}
int[] t = a; a = b; b = t;
int o = ao; ao = bo; bo = o;
}
統計排序
統計排序適用於元素個數遠大於元素種數的情況,適用於Short、Byte、Char等元素種數較少的類型。如下代碼以Short為例執行統計排序。
int[] count = new int[NUM_SHORT_VALUES];
for (int i = left - 1; ++i <= right;
count[a[i] - Short.MIN_VALUE]++
);
for (int i = NUM_SHORT_VALUES, k = right + 1; k > left; ) {
while (count[--i] == 0);
short value = (short) (i + Short.MIN_VALUE);
int s = count[i];
do {
a[--k] = value;
} while (--s > 0);
}
總結
Array.sort()函數很難說使用了哪種排序算法,因為它用了好幾種排序算法。根本原因在於不同的排序算法有不同的使用場景。Array.sort()函數定義了一系列經驗得出的常量實現了算法路由,這是值得借鑒的地方。
參考資料
https://baike.baidu.com/item/TimSort/10279720?fr=aladdin
https://www.jianshu.com/p/2c6f79e8ce6e
https://www.jianshu.com/p/2c6f79e8ce6e