事實上Collections.sort方法底層就是調用的array.sort方法,並且不管是Collections.sort或者是Arrays.sort方法,java
跟蹤下源代碼吧,首先咱們寫個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); } }
簡單得不能再簡單的方法了,讓咱們一步步跟蹤數組
咱們沒有寫比較器,因此用的第二項,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. 反正是一種老的歸併排序,不用管了如今默認是關的
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; }
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