Collections.sort() and Arrays.sort() sorting algorithm choices

During the interview today, I was asked about the sorting algorithm implementation of Collections.sort();the Arrays.sort();two methods. I only remember that one is quick sort and the other is merge sort, but I just saw that it is not so simple.

Arrays.sort()

Let's take a look first Arrays.sort();, a little bit into this method will see something like this

public static void sort(int[] a) {
    DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
}
  • 1
  • 2
  • 3

Sure enough, it's not that simple. DualPivotQuicksortThe translation is dual-axis quick sort. For dual-axis sorting, you can go here http://www.cnblogs.com/nullzx/p/5880191.html to see. Then click in again, you can find that there is such a piece of code

if (right - left < QUICKSORT_THRESHOLD) {
    sort(a, left, right, true);
    return;
}
  • 1
  • 2
  • 3
  • 4

It can be found that if the length of the array is less than QUICKSORT_THRESHOLDthis, this dual-axis quick sort will be used, and this value is 286.

If it is greater than 286, it will insist on the continuous ascending and descending order of the array.

 * The array is not highly structured,
 * use Quicksort instead of merge sort.
  • 1
  • 2
  • 3

Now go back to the above decision to use the dual-axis quick sort method, and then click in, and find that there will be one more judgment

// Use insertion sort on tiny arrays
if (length < INSERTION_SORT_THRESHOLD)
  • 1
  • 2
  • 3

That is, if the length of the array is less than INSERTION_SORT_THRESHOLD(the value is 47), then insertion sort will be used, otherwise dual-axis quick sort will be used.

So to summarize the Arrays.sort() method, if the length of the array is greater than or equal to 286 and the continuity is good, use merge sort, if it is greater than or equal to 286 and the continuity is not good, use dual-axis quick sort. If the length is less than 286 and greater than or equal to 47, use dual-axis quicksort, and if the length is less than 47, use insertion sort. It's quite wrapping~

Collections.sort()

Let's take a look again Collections.sort(), click all the way in, and find that you will enter Arrays, let's see what options are there

public static <T> void sort(T[] a, Comparator<? super T> c) {
    if (c == null) {
        sort(a);
    } else {
        if (LegacyMergeSort.userRequested)
            legacyMergeSort(a, c);
        else
            TimSort.sort(a, 0, a.length, c, null, 0, 0);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

You will find that if it LegacyMergeSort.userRequestedis true, merge sort will be used, which can be set to true by the following code

System.setProperty("java.util.Arrays.useLegacyMergeSort", "true"); 
  • 1
  • 2

However, there is a sentence in the annotation of the method legacyMergeSort, indicating that the traditional merge may be removed in the future.

/** To be removed in a future release. */
  • 1
  • 2

If it is not true, a so-called TimSortsorting algorithm will be used. This algorithm can be found here http://blog.csdn.net/yangzhongblog/article/details/8184707 .

Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/TimHeath/article/details/68930482

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325442604&siteId=291194637