Arrays.sort如何實(shí)現(xiàn)降序排序
Arrays.sort實(shí)現(xiàn)降序排序
在調(diào)用Arrays.sort()對(duì)數(shù)組進(jìn)行排序時(shí),默認(rèn)是升序排序的,如果想讓數(shù)組降序排序,有下面兩種方法:
1.Collections的reverseOrder
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); ? ? ? ? } ? ? } }
2.利用Comparator接口復(fù)寫(xiě)compare
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 //可以去掉。作用是檢查下面的方法名是不是父類(lèi)中所有的 ? ? public int compare(Integer a,Integer b){ // ? ? ? ?兩種都可以,升序排序的話(huà)反過(guò)來(lái)就行 // ? ? ? ?return a-b<0?1:-1; ? ? ? ? return b-a; ? ? } }
注意:如果需要改變默認(rèn)的排列方式,不能使用基本類(lèi)型(int,char等)定義變量,而應(yīng)該用對(duì)應(yīng)的類(lèi)
Arrays.sort底層原理
概述
Collections.sort()方法底層調(diào)用的也是Arrays.sort()方法,下面我們通過(guò)測(cè)試用例debug,探究一下其源碼,首先說(shuō)一下結(jié)果,使用到了插入排序,雙軸快排,歸并排序
雙軸快排(DualPivotQuicksort): 顧名思義有兩個(gè)軸元素pivot1,pivot2,且pivot ≤
pivot2,將序列分成三段:x < pivot1、pivot1 ≤ x ≤ pivot2、x >pivot2,然后分別對(duì)三段進(jìn)行遞歸。這個(gè)算法通常會(huì)比傳統(tǒng)的快排效率更高,也因此被作為Arrays.java中給基本類(lèi)型的數(shù)據(jù)排序的具體實(shí)現(xiàn)。
大致流程:
快速排序部分展開(kāi)
案例
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); }
運(yùn)行結(jié)果
1 進(jìn)入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 進(jìn)入DualPivotQuicksort類(lèi)內(nèi)部的靜態(tài)方法sort
方法上的注釋
3 走sort的流程
1. 排序范圍小于286的數(shù)組使用快速排序
// Use Quicksort on small arrays if (right - left < QUICKSORT_THRESHOLD) { sort(a, left, right, true); return; } // Merge sort ......
2. 進(jìn)入sort方法,判斷數(shù)組長(zhǎng)度是否小于47,小于則直接采用插入排序,否則執(zhí)行3。
// Use insertion sort on tiny arrays if (length < INSERTION_SORT_THRESHOLD) { // Insertion sort ...... }
3. 用公式length/8+length/64+1近似計(jì)算出數(shù)組長(zhǎng)度的1/7。
// Inexpensive approximation of length / 7 int seventh = (length >> 3) + (length >> 6) + 1;
4. 取5個(gè)根據(jù)經(jīng)驗(yàn)得出的等距點(diǎn)。
/* * 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個(gè)元素進(jìn)行插入排序
// 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進(jìn)行了排序,所以必有pivot1 <=pivot2。定義兩個(gè)指針less和great,less從最左邊開(kāi)始向右遍歷,一直找到第一個(gè)不小于pivot1的元素,great從右邊開(kāi)始向左遍歷,一直找到第一個(gè)不大于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開(kāi)始向右遍歷至great,把小于pivot1的元素移動(dòng)到less左邊,大于pivot2的元素移動(dòng)到great右邊。這里要注意,我們已知great處的元素小于pivot2,但是它于pivot1的大小關(guān)系,還需要進(jìn)行判斷,如果比pivot1還小,需要移動(dòng)到到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. 對(duì)于中間的部分,如果大于4/7的數(shù)組長(zhǎng)度,遞歸中間部分
/* * 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 小結(jié)
Arrays.sort對(duì)升序數(shù)組、降序數(shù)組和重復(fù)數(shù)組的排序效率有了很大的提升,這里面有幾個(gè)重大的優(yōu)化。
- 對(duì)于小數(shù)組來(lái)說(shuō),插入排序效率更高,每次遞歸到小于47的大小時(shí),用插入排序代替快排,明顯提升了性能。
- 雙軸快排使用兩個(gè)pivot,每輪把數(shù)組分成3段,在沒(méi)有明顯增加比較次數(shù)的情況下巧妙地減少了遞歸次數(shù)。
- pivot的選擇上增加了隨機(jī)性,卻沒(méi)有帶來(lái)隨機(jī)數(shù)的開(kāi)銷(xiāo)。
- 對(duì)重復(fù)數(shù)據(jù)進(jìn)行了優(yōu)化處理,避免了不必要交換和遞歸。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java Swing GridBagLayout網(wǎng)格袋布局的實(shí)現(xiàn)
這篇文章主要介紹了Java Swing GridBagLayout網(wǎng)格袋布局的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12使用Java如何將圖片轉(zhuǎn)成Base64編碼,并壓縮至40k
這篇文章主要介紹了使用Java如何將圖片轉(zhuǎn)成Base64編碼,并壓縮至40k問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Hibernate中l(wèi)oad方法與get方法的區(qū)別
Hibernate中有兩個(gè)極為相似的方法get()與load(),他們都可以通過(guò)指定的實(shí)體類(lèi)與ID從數(shù)據(jù)庫(kù)中讀取數(shù)據(jù),并返回對(duì)應(yīng)的實(shí)例,但Hibernate不會(huì)搞兩個(gè)完全一樣的方法的2016-01-01Java中向文件寫(xiě)入數(shù)據(jù)的幾種常見(jiàn)方式分享
在日常開(kāi)發(fā)中,肯定離不開(kāi)要和文件打交道,今天就簡(jiǎn)單羅列一下平時(shí)比較常用的創(chuàng)建文件并向文件中寫(xiě)入數(shù)據(jù)的幾種方式,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-10-10基于Java 數(shù)組內(nèi)存分配的相關(guān)問(wèn)題
本篇文章是對(duì)Java中數(shù)組內(nèi)存分配進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05