<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
在呼叫Arrays.sort()對陣列進行排序時,預設是升序排序的,如果想讓陣列降序排序,有下面兩種方法:
import java.util.*; public class Main { public static void main(String[] args) { // 注意這裡是Integer,不是int Integer[] arr={9,8,7,6,5,4,3,2,1}; Arrays.sort(arr,Collections.reverseOrder()); for(int i:arr){ System.out.println(i); } } }
import java.util.*; public class Main { public static void main(String[] args) { Integer[] arr={9,8,7,6,5,4,3,2,1}; Comparator cmp=new CMP(); Arrays.sort(arr,cmp); for(int i:arr){ System.out.println(i); } } } class CMP implements Comparator<Integer>{ @Override //可以去掉。作用是檢查下面的方法名是不是父類別中所有的 public int compare(Integer a,Integer b){ // 兩種都可以,升序排序的話反過來就行 // return a-b<0?1:-1; return b-a; } }
注意:如果需要改變預設的排列方式,不能使用基本型別(int,char等)定義變數,而應該用對應的類
Collections.sort()方法底層呼叫的也是Arrays.sort()方法,下面我們通過測試用例debug,探究一下其原始碼,首先說一下結果,使用到了插入排序,雙軸快排,歸併排序
雙軸快排(DualPivotQuicksort): 顧名思義有兩個軸元素pivot1,pivot2,且pivot ≤
pivot2,將序列分成三段:x < pivot1、pivot1 ≤ x ≤ pivot2、x >pivot2,然後分別對三段進行遞迴。這個演演算法通常會比傳統的快排效率更高,也因此被作為Arrays.java中給基本型別的資料排序的具體實現。
大致流程:
快速排序部分展開
public static void main(String[] args) { int[] nums = new int[]{6,5,4,3,2,1}; List<Integer> list = Arrays.asList(6, 5, 4, 3, 2, 1); Arrays.sort(nums); Collections.sort(list); System.out.println(Arrays.toString(nums)); System.out.println(list); }
執行結果
1 進入Arrays.sort()方法
/** * Sorts the specified array into ascending numerical order. * * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm * offers O(n log(n)) performance on many data sets that cause other * quicksorts to degrade to quadratic performance, and is typically * faster than traditional (one-pivot) Quicksort implementations. * * @param a the array to be sorted */ public static void sort(int[] a) { DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); }
方法上的註釋
2 進入DualPivotQuicksort類內部的靜態方法sort
方法上的註釋
3 走sort的流程
1. 排序範圍小於286的陣列使用快速排序
// Use Quicksort on small arrays if (right - left < QUICKSORT_THRESHOLD) { sort(a, left, right, true); return; } // Merge sort ......
2. 進入sort方法,判斷陣列長度是否小於47,小於則直接採用插入排序,否則執行3。
// Use insertion sort on tiny arrays if (length < INSERTION_SORT_THRESHOLD) { // Insertion sort ...... }
3. 用公式length/8+length/64+1近似計算出陣列長度的1/7。
// Inexpensive approximation of length / 7 int seventh = (length >> 3) + (length >> 6) + 1;
4. 取5個根據經驗得出的等距點。
/* * Sort five evenly spaced elements around (and including) the * center element in the range. These elements will be used for * pivot selection as described below. The choice for spacing * these elements was empirically determined to work well on * a wide variety of inputs. */ int e3 = (left + right) >>> 1; // The midpoint int e2 = e3 - seventh; int e1 = e2 - seventh; int e4 = e3 + seventh; int e5 = e4 + seventh;
5.將這5個元素進行插入排序
// 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; } } } }
6. 選取a[e2],a[e4]分別作為pivot1,pivot2。由於步驟5進行了排序,所以必有pivot1 <=pivot2。定義兩個指標less和great,less從最左邊開始向右遍歷,一直找到第一個不小於pivot1的元素,great從右邊開始向左遍歷,一直找到第一個不大於pivot2的元素。
/* * Use the second and fourth of the five sorted elements as pivots. * These values are inexpensive approximations of the first and * second terciles of the array. Note that pivot1 <= pivot2. */ int pivot1 = a[e2]; int pivot2 = a[e4]; /* * The first and the last elements to be sorted are moved to the * locations formerly occupied by the pivots. When partitioning * is complete, the pivots are swapped back into their final * positions, and excluded from subsequent sorting. */ a[e2] = a[left]; a[e4] = a[right]; /* * Skip elements, which are less or greater than pivot values. */ while (a[++less] < pivot1); while (a[--great] > pivot2);
7. 接著定義指標k從less-1開始向右遍歷至great,把小於pivot1的元素移動到less左邊,大於pivot2的元素移動到great右邊。這裡要注意,我們已知great處的元素小於pivot2,但是它於pivot1的大小關係,還需要進行判斷,如果比pivot1還小,需要移動到到less左邊,否則只需要交換到k處。
/* * Partitioning: * * 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; ) { short 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; } }
8. 將樞軸交換到它們的最終位置
// Swap pivots into their final positions a[left] = a[less - 1]; a[less - 1] = pivot1; a[right] = a[great + 1]; a[great + 1] = pivot2;
9. 遞迴排序左右部分,不包括已知的樞軸
// Sort left and right parts recursively, excluding known pivots sort(a, left, less - 2, leftmost); sort(a, great + 2, right, false);
10. 對於中間的部分,如果大於4/7的陣列長度,遞迴中間部分
/* * If center part is too large (comprises > 4/7 of the array), * swap internal pivot values to ends. */ 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; ) { short 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);
4 小結
Arrays.sort對升序陣列、降序陣列和重複陣列的排序效率有了很大的提升,這裡面有幾個重大的優化。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45