Arrays.sort和Collections.sort實現原理解析

Arrays.sort和Collections.sort實現原理解析

一、使用

  • 排序

二、原理

  1. 事實上Collections.sort方法底層就是調用的array.sort方法,並且不管是Collections.sort或者是Arrays.sort方法,java

  2. 跟蹤下源代碼吧,首先咱們寫個demo算法

public static void main(String[] args) { List<String> strings = Arrays.asList("6", "1", "3", "1","2"); Collections.sort(strings);//sort方法在這裏 for (String string : strings) { System.out.println(string); } }

 

簡單得不能再簡單的方法了,讓咱們一步步跟蹤數組

  1. OK,往下面看,發現collections.sort方法調用的list.sort

QQ20170221-0@2x

  1. 而後跟蹤一下,list裏面有個sort方法,可是list是一個接口,確定是調用子類裏面的實現,這裏咱們demo使用的是一個Arrays.asList方法,因此事實上咱們的子類就是arraylist了。OK,看arraylist裏面sort實現,選擇第一個,爲何不選擇第二個呢?(能夠看二樓評論,解答得很正確,簡單說就是用Arrays.sort建立的ArrayList對象)

QQ20170221-1@2x

  1. OK,發現裏面調用的Arrays.sort(a, c); a是list,c是一個比較器,咱們來看一下這個方法 
    QQ20170221-2@2x

咱們沒有寫比較器,因此用的第二項,LegacyMergeSort.userRequested這個bool值是什麼呢? 
跟蹤這個值,咱們發現有這樣的一段定義:markdown

> Old merge sort implementation can be selected (for
  >  compatibility with broken comparators) using a system property.
  >  Cannot be a static boolean in the enclosing class due to
  >  circular dependencies. To be removed in a future release.

  反正是一種老的歸併排序,不用管了如今默認是關的
  1. OK,咱們走的是sort(a)這個方法,接着進入這個 
    QQ20170221-3@2x
  2. 接着看咱們重要的sort方法
static void sort(Object[] a, int lo, int hi, Object[] work, int workBase, int workLen) { assert a != null && lo >= 0 && lo <= hi && hi <= a.length; int nRemaining = hi - lo; if (nRemaining < 2) return; // array的大小爲0或者1就不用排了 // 當數組大小小於MIN_MERGE(32)的時候,就用一個"mini-TimSort"的方法排序,jdk1.7新加 if (nRemaining < MIN_MERGE) { //這個方法比較有意思,其實就是將咱們最長的遞減序列,找出來,而後倒過來 int initRunLen = countRunAndMakeAscending(a, lo, hi); //長度小於32的時候,是使用binarySort的 binarySort(a, lo, hi, lo + initRunLen); return; } //先掃描一次array,找到已經排好的序列,而後再用剛纔的mini-TimSort,而後合併,這就是TimSort的核心思想 ComparableTimSort ts = new ComparableTimSort(a, work, workBase, workLen); int minRun = minRunLength(nRemaining); do { // Identify next run int runLen = countRunAndMakeAscending(a, lo, hi); // If run is short, extend to min(minRun, nRemaining) if (runLen < minRun) { int force = nRemaining <= minRun ? nRemaining : minRun; binarySort(a, lo, lo + force, lo + runLen); runLen = force; } // Push run onto pending-run stack, and maybe merge ts.pushRun(lo, runLen); ts.mergeCollapse(); // Advance to find next run lo += runLen; nRemaining -= runLen; } while (nRemaining != 0); // Merge all remaining runs to complete sort assert lo == hi; ts.mergeForceCollapse(); assert ts.stackSize == 1; }
  1. 回到5,咱們能夠看到當咱們寫了比較器的時候就調用了TimSort.sort方法,源碼以下
static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c, T[] work, int workBase, int workLen) { assert c != null && a != null && lo >= 0 && lo <= hi && hi <= a.length; int nRemaining = hi - lo; if (nRemaining < 2) return; // Arrays of size 0 and 1 are always sorted // If array is small, do a "mini-TimSort" with no merges if (nRemaining < MIN_MERGE) { int initRunLen = countRunAndMakeAscending(a, lo, hi, c); binarySort(a, lo, hi, lo + initRunLen, c); return; } /** * March over the array once, left to right, finding natural runs, * extending short natural runs to minRun elements, and merging runs * to maintain stack invariant. */ TimSort<T> ts = new TimSort<>(a, c, work, workBase, workLen); int minRun = minRunLength(nRemaining); do { // Identify next run int runLen = countRunAndMakeAscending(a, lo, hi, c); // If run is short, extend to min(minRun, nRemaining) if (runLen < minRun) { int force = nRemaining <= minRun ? nRemaining : minRun; binarySort(a, lo, lo + force, lo + runLen, c); runLen = force; } // Push run onto pending-run stack, and maybe merge ts.pushRun(lo, runLen); ts.mergeCollapse(); // Advance to find next run lo += runLen; nRemaining -= runLen; } while (nRemaining != 0); // Merge all remaining runs to complete sort assert lo == hi; ts.mergeForceCollapse(); assert ts.stackSize == 1; }

和上面的sort方法是同樣的,其實也就是TimSort的源代碼post

三、總結

不管是Collections.sort方法或者是Arrays.sort方法,底層實現都是TimSort實現的,這是jdk1.7新增的,之前是歸併排序。TimSort算法就是找到已經排好序數據的子序列,而後對剩餘部分排序,而後合併起來spa

 轉載:https://blog.csdn.net/u011410529/article/details/56668545
相關文章
相關標籤/搜索