DualPivotQuickSort聚集了多種排序算法,稱之爲DualPivotQuickSort並不合適。不一樣的排序算法有不一樣的使用場景。看懂此文件,排序算法就算完全搞懂了。
本文只介紹有用的代碼片斷,DualPivotQuickSort.java能夠用這些代碼片斷拼湊起來。
本文中的排序對數組a的[left,right]閉區間進行排序。java
常量中除了QUICKSORT_THRESHOLD其他都是用來選擇排序算法的,選擇排序算法主要考慮元素個數。算法
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表示第一個元素。app
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執行快排。less
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值覆蓋某個數字,可能形成排序後的數組中的值發生變化。ui
雙軸快排就是使用兩個pivot劃分數組,把數組分爲(負無窮,pivot1)、[pivot1,pivot2]、(pivot2,正無窮)三部分。pivot1和pivot2取a[e2]和a[e4]。spa
//選擇兩個pivot int pivot1 = a[e2]; int pivot2 = a[e4]; //把left和right放在e二、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與內存之間的數據流量的大小也就把這個比較慢的內存的因素考慮進去了,所以也就比元素比較次數更能體現算法在當下計算機裏面的性能指標。
如下代碼把數組劃分紅若干個遞增片斷,若是遇到遞減片斷會嘗試翻轉數組使之遞增。
若是遞增片斷太多(超過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