How does Java provide a sort algorithm to implement? __ Code

Source: Internet
Author: User

A few days ago, a set of interview questions, one of the problems is the Java JDK we see the Collections.sort () and Arrays.sort () the implementation of the two algorithms is what, many small partners in the heart of the default should be fast-row, But not full or understanding is not deep enough, below we from the source level a little bit to explain this problem: First, Arrays.sort () sorting algorithm

Let's take a look at Arrays.sort (), the sort method has a lot of overloads, there are more than 10 kinds, with int view as follows:

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

You can see there's a dualpivotquicksort,dualpivotquicksort translation here is a double-axis quick sort (about double axes fast sort our later discussion can be considered an improvement on our common use of the fast-row, and another improvement is the three-way fast-row. And once again, you can see that there is this piece of code:

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

It is found 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 determine whether the array of continuous ascending and continuous descending is good, if the good words are sorted with the merge, 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 click In, and found that there will be one more judgment:

Use insertion the 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.

In summary, if the length of the array is greater than 286 and the continuity is good, it is sorted by merge, if it is greater than 286 and the continuity is not good, the two axes are sorted quickly. 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. The schematic diagram is as follows:
two, Collections.sort () The sorting algorithm

Take a look collections.sort (), step by step in, found that will go into the arrays:

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.

If not true, it will use a sort algorithm called Timsort, this algorithm is interested to understand. Third, summary

How to kill people in the interview, when asked this question, we will not blurt out just a quick row.

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.