Collections.sort () and Arrays.sort () sorting algorithm select __ algorithm

Source: Internet
Author: User
Tags array length

SOURCE Location: Click Open Link Click Open link

Arrays.sort ()

Let's take a look at Arrays.sort ();

public static void sort (int[] a) {
    dualpivotquicksort.sort (A, 0, a.length-1, NULL, 0, 0);
}

It is not so simple, dualpivotquicksort translation is a two-axis fast sort, about the two-axis sort can go here http://www.cnblogs.com/nullzx/p/5880191.html see. Then again, you can find a piece of code.

if (Right-left < Quicksort_threshold) {
    sort (a, left, right, true);
    return;
}

You can see that if the length of the array is less than Quicksort_threshold, the two-axis quick sort is used, and the value is 286.

If it is greater than 286, it will adhere to the array of continuous ascending and continuous descending of the good, if the good words with the merge sort, the bad words with a quick sort, look at the following note can be seen

* The array is not highly structured,
 * Use Quicksort instead of the merge sort.

So now go back to the decision to use the two-axis fast sorting method, and then point in, found that there will be one more judgment

Use insertion sort on tiny arrays
if (length < insertion_sort_threshold)

That is, if the array length is less than insertion_sort_threshold (the value is 47), then the insertion sort is used, or the double axis is used to quickly sort.

So summing up the Arrays.sort () method, if the length of the array is greater than 286 and the continuity is good, then use the merge sort, if more than 286 and the continuity is not good, the two-axis fast sorting. If the length is less than 286 and greater than or equal to 47, the two-axis quick sort is used, and if the length is less than 47, the insertion is sorted. It's a circle.

Summary: lenthth<47 insert Sort

47<length<286 Double axes quick sort

length>286 and continuity is not good. Double-axis Quick Sort

length>286 and continuous good merge sort Collections.sort ()

Look again Collections.sort (), all the way to go in, found that will go into the arrays, to see what choice

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);
    }

You will find that if legacymergesort.userrequested is true, the merge sort is used, and you can set the following code to True

However, the comment on the method Legacymergesort has this remark that the traditional merge may be removed later.

/** to is removed in a future release. */

If not true, it will use a sort algorithm called Timsort, this algorithm can go here http://blog.csdn.net/yangzhongblog/article/details/8184707 to see.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.