Java Collection (14)--double pivot quick sort (dualpivotquicksort)

Source: Internet
Author: User

JDK1.7 java.uti.Arrays start using dualpivotquicksort as the default sorting method

Detailed Links: http://www.tuicool.com/articles/BfY7Nz

Algorithm idea:

Two pivot P1 and P2are selected, requiring 3 pointer l,k,g.

The functions of 3 pointers are as follows:

The algorithm is for the following steps: (using dualpivotquicksortwhen the array size is less than 286 )

1, smaller than the array, using the insertion sort.

2, select Pivot P1 and P2. (Suppose you use array headers and tails).

3, P1 need less than P2, no exchange.

Now the array is divided into 4 parts, left to l the number of less than P1, l to K is greater than P1 less than P2 number, G to rigth greater than the number of P2, pending K to G in the middle of the number (gradually processed into the first 3 regions).

Key Algorithms Section

4, L from start initialization until not less than p1,k initialized to l-1,g from the end of the initialization until not much more than P2. K is a pointer to the main movement, to devour the middle area step-by-step.

When greater than P1 is less than p2,k++.

When less than P1, the number of exchanges L and K, l++,k++.

When greater than P2, if the number of G is less than P1, the number on the L on K, put the number of G on L, l++, and then put K before the number of G, g--,k++, complete a l,k,g exchange. Otherwise exchange K and G, and g--,k++.

5, recursion 4.

6, Exchange P1 to L-1. Swap P2 to g+1.

7, recursion.

Flow chart:

Java Collection (14)--double pivot quick sort (dualpivotquicksort)

Related Article

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.