Java1.7之后Arrays.sort对数组排序DualPivotQuicksort.sort
生活随笔
收集整理的這篇文章主要介紹了
Java1.7之后Arrays.sort对数组排序DualPivotQuicksort.sort
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有粉絲叫我幫他做一下這道題。。。。
?額。。。。。。這同學應該好好聽課啦 哈哈
int[] a = {25, 24, 12, 76, 101, 96, 28};Arrays.sort(a);//排序System.out.println("排序后數組如下");for (int i = 0; i < a.length; i++) {System.out.print(a[i] + " ,");}既然寫到這里了就看下底層實現吧
斷點跟蹤調用的是DualPivotQuicksort.java類的java雙基準快速排序方法sort實現
?跟蹤進去就是具體排序方法的實現、其中具體方法:參數 int[] a是需被排序的int數組, left和right是該數組中需要被排序的部分的左右界限. 而后面的work, workBase和workLen三個參數其實并不會參與雙基準快速排序, 而是當系統認為本數組更適合使用歸并排序(merge sort)的時候, 供歸并排序使用。具體代碼算法源碼如下。
static void sort(int[] a, int left, int right,int[] work, int workBase, int workLen) {// Use Quicksort on small arraysif (right - left < QUICKSORT_THRESHOLD) {sort(a, left, right, true);return;}/** Index run[i] is the start of i-th run* (ascending or descending sequence).*/int[] run = new int[MAX_RUN_COUNT + 1];int count = 0; run[0] = left;// Check if the array is nearly sortedfor (int k = left; k < right; run[count] = k) {if (a[k] < a[k + 1]) { // ascendingwhile (++k <= right && a[k - 1] <= a[k]);} else if (a[k] > a[k + 1]) { // descendingwhile (++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 { // equalfor (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {if (--m == 0) {sort(a, left, right, true);return;}}}/** The array is not highly structured,* use Quicksort instead of merge sort.*/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 elementrun[++count] = right;} else if (count == 1) { // The array is already sortedreturn;}// Determine alternation base for mergebyte odd = 0;for (int n = 1; (n <<= 1) < count; odd ^= 1);// Use or create temporary array b for mergingint[] b; // temp array; alternates with aint ao, bo; // array offsets from 'left'int blen = right - left; // space needed for bif (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;}// Mergingfor (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;}}雖然看似簡單的一行代碼其實底層的實現也是很復雜的、對算法感興趣的同學可以仔細看看底層實現。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Java1.7之后Arrays.sort对数组排序DualPivotQuicksort.sort的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于Java+SpringBoot+vu
- 下一篇: 《SpringCloud超级入门》使用E