java1.7之后的版本,开始用双轴快排取代了以前的排序算法,现在只实现了8种基本数据类型性的双轴快排(降序排序),对象的排序在1.7中还在用老式的,不过都标了过时,估计以后版本中就会被新的双轴快排取代了。
DualPivotQuicksort()方法,里边一共写了三种算法(不算改进版的插入排序),对于大数组而且部分高度有序的用归并排序;其余的用双轴快排进行分割,分割到足够小的时候用插入排序(主要是改进版的pair insertion sort)。
DualPivotQuicksort()方法,里边一共写了三种算法(不算改进版的插入排序),对于大数组而且部分高度有序的用归并排序;其余的用双轴快排进行分割,分割到足够小的时候用插入排序(主要是改进版的pair insertion sort)。
双轴快排的基本原理是取两个pivot,所有比pivot1小的放到最左边,比pivot2大的放到最右边,然后递归下去,就可以把两端的元素完成排序,之后处理中间部分,中间部分如果过大就继续递归用这种方式继续分割,如果不大,就用单轴分割对两部分递归调用下去。
public static void sort(int[] a) { DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); }
// 双轴快排
static void sort(int[] a, int left, int right, int[] work, int workBase, int workLen) { // Use Quicksort on small arrays if (right - left < QUICKSORT_THRESHOLD) {
// 数组长度小于286时,直接用快速排序 sort(a, left, right, true); return; }
/* * run[i]记录第i次run的开始点(升序或降序) */ 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) { 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]); for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) { int t = a[lo]; a[lo] = a[hi]; a[hi] = t; } } else { // equal for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) { if (--m == 0) { sort(a, left, right, true); return; } } }
/* * 如果数组不是高度有序,用快排代替归并 */ if (++count == MAX_RUN_COUNT) { sort(a, left, right, true); return; } }
// Check special cases // Implementation note: variable "right" is increased by 1. if (run[count] == right++) { // The last run contains one element run[++count] = right; } else if (count == 1) { // The array is already sorted return; }
// Determine alternation base for merge byte odd = 0; for (int n = 1; (n <<= 1) < count; odd ^= 1);
// 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; } }
// 快速排序
private static void sort(int[] a, int left, int right, boolean leftmost) { int length = right - left + 1;
// 对于短数组用插入排序 if (length < INSERTION_SORT_THRESHOLD) { //47 if (leftmost) { for (int i = left, j = i; i < right; j = ++i) { int ai = a[i + 1]; while (ai < a[j]) { a[j + 1] = a[j]; if (j-- == left) { break; } } a[j + 1] = ai; } } else { /* * Skip the longest ascending sequence. */ do { if (left >= right) { return; } } while (a[++left] >= a[left - 1]);
/*
* 来自相邻部分的每个元素都扮演哨兵的角色,因此我们在每次迭代中可以避免左距离检查。
* 此外,我们使用了更优化的算法,也就是所谓的对插入排序,它比插入排序的传统实现更快(在快速排序的环境中)。
*/ for (int k = left; ++left <= right; k = ++left) { int 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; } int last = a[right]; while (last < a[--right]) { a[right + 1] = a[right]; } a[right + 1] = last; } return; }
// length / 7 的近似值 int seventh = (length >> 3) + (length >> 6) + 1;
/*
* 将5个均匀间隔的元素(包括)在范围内的中心元素排序。这些元素将用于下面描述的支点。
* 根据经验,对这些元素间距的选择,在各种各样的输入上都能很好地工作。
*/ int e3 = (left + right) >>> 1; // 中点 int e2 = e3 - seventh; int e1 = e2 - seventh; int e4 = e3 + seventh; int e5 = e4 + seventh;
// 用插入排序将这5各元素进行排序 if (a[e2] < a[e1]) { int t = a[e2]; a[e2] = a[e1]; a[e1] = t; } if (a[e3] < a[e2]) { int 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]) { int 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]) { int 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; } } } }
// 指针 int less = left;
// 中心部分的第一个元素的index int great = right;
// 右部分第一个元素的前一个index if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
/*
* 数组第二个和第四个取出来做pivot。
* 注意:pivot1 <= pivot2.
*/ int pivot1 = a[e2]; int pivot2 = a[e4];
/*
* 第一个和最后一个要排序的元素被移动到以前由pivot占据的位置。
* 当分隔完成时,pivot被交换回它们的最终位置,并被排除在后续排序之外。
*/ a[e2] = a[left]; a[e4] = a[right];
/*
* 跳过比pivot大或小的元素,跳过左边比pivot1小的元素和右边比pivot大的元素,也就是这些元素不用动了,放在原地就好。
*/ while (a[++less] < pivot1); while (a[--great] > pivot2);
/*
* 分割过程:
*
* left part center part right part
* +--------------------------------------------------------------+
* | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
* +--------------------------------------------------------------+
* ^ ^ ^
* | | |
* less k great
*
* Invariants:
*
* all in (left, less) < pivot1
* pivot1 <= all in [less, k) <= pivot2
* all in (great, right) > pivot2
*
* Pointer k is the first index of ?-part.
*/ 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];
/*
* Here and below we use "a[i] = b; i++;" instead
* of "a[i++] = b;" due to performance issue.
*/ 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]; }
/*
* Here and below we use "a[i] = b; i--;" instead
* of "a[i--] = b;" due to performance issue.
*/ a[great] = ak; --great; } }
// 交换pivot到它们最终应该在的位置 a[left] = a[less - 1]; a[less - 1] = pivot1; a[right] = a[great + 1]; a[great + 1] = pivot2;
// 排除掉已知的pivot,递归排序左边和右边部分 sort(a, left, less - 2, leftmost); sort(a, great + 2, right, false);
/*
* 上边的递归结束,左边的和右边就已经排完了,而且都在在自己该在的位置,
* 接下来就是中间的部分,下面判断中间部分范围大小,
* 如果大于七分之四(e1和e5的作用在这才看出来),就把所有和指针相等的元素也扔过去然后对这一部分递归,
* 算法和上边一样,只是<>改成了=
*/ if (less < e1 && e5 < great) { /* * Skip elements, which are equal to pivot values. */ while (a[less] == pivot1) { ++less; } while (a[great] == pivot2) { --great; }
/*
* Partitioning:
*
* left part center part right part
* +----------------------------------------------------------+
* | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
* +----------------------------------------------------------+
* ^ ^ ^
* | | |
* less k great
*
* Invariants:
*
* all in (*, less) == pivot1
* pivot1 < all in [less, k) < pivot2
* all in (great, *) == pivot2
*
* Pointer k is the first index of ?-part.
*/ 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];
/*
* Even though a[great] equals to pivot1, the
* assignment a[less] = pivot1 may be incorrect,
* if a[great] and pivot1 are floating-point zeros
* of different signs. Therefore in float and
* double sorting methods we have to use more
* accurate assignment a[less] = a[great].
*/ a[less] = pivot1; ++less; } else {
// pivot1 < a[great] < pivot2 a[k] = a[great]; } a[great] = ak; --great; } } }
// Sort center part recursively sort(a, less, great, false); } else {
// Partitioning with one pivot
/*
* 如果不算很大,以中间e3作为pivot,一样的算法排
*/ int pivot = a[e3];
/*
* Partitioning degenerates to the traditional 3-way
* (or "Dutch National Flag") schema:
*
* left part center part right part
* +-------------------------------------------------+
* | < pivot | == pivot | ? | > pivot |
* +-------------------------------------------------+
* ^ ^ ^
* | | |
* less k great
*
* Invariants:
*
* all in (left, less) < pivot
* all in [less, k) == pivot
* all in (great, right) > pivot
*
* Pointer k is the first index of ?-part.
*/ for (int k = less; k <= great; ++k) { if (a[k] == pivot) { continue; } int 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 left and right parts recursively.
* All elements from center part are equal
* and, therefore, already sorted.
*/ sort(a, left, less - 1, leftmost); sort(a, great + 1, right, false); } }