Quick sort of deep Dutch flag issue

Source: Internet
Author: User

I. Preamble

When using the partition-exchange sorting algorithm, such as a fast sort algorithm (even if a good key element pivot values is selected), we often face a very awkward situation-when there are many duplicate elements in the Sort object, The Partition-exchange sorting algorithm is not very satisfactory. This is especially easy to understand when all the elements are equal. In each recursion, the left part is empty (no element is smaller than the key element), and the right part can only be moved one decrement. As a result, it takes two times to sort the equal elements. In order to solve this problem (sometimes called the Dutch flag issue), we describe in detail the way to solve this problem.

Ii. description of the issue of "the Dutch flag problem"

The "Dutch flag problem" is a procedural problem in computer science, which is proposed by Edsger Dijkstra. The Dutch flag is made up of red, white and blue colors.

Now there are a number of red, white, and blue colors of the ball randomly arranged into a straight line. Now our task is to sort the balls in red, white and blue.

Three, the problem analysis

  

This problem can be considered as an array sorting problem, the array is divided into the front, the middle and the back of the three parts, each element (red and white blue corresponding to 0, 1, 2) must belong to one of them. Because the number of red, white, and blue balls is not necessarily the same, so this three region is not necessarily equal, that is, if we place the entire area in [0,1] area, due to the number of the three-color ball between the difference (here is assumed 1:2:2), the front is [0,0.2], the middle is [ 0.2,0.6), and the rear part is [0.6,1]. Our ideas are as follows: The front and back are lined up in front and rear of the array, and the middle is naturally lined up. Specific to:
Set two flag bits begin and end point to the beginning and end of the array, and then use a flag bit current to traverse from the beginning:
1) If the traverse to the position is 0, then it must belong to the front, so it is exchanged with the begin position, and then the current forward, begin also moves (indicating that the front is already lined up).
2) If the Traverse to the position of 1, then it must belong to the middle, according to the general idea, we are not moving in the middle, then current forward.
3) If the traverse to the position of 2, then it must belong to the rear, so it is exchanged with the end position, since the exchange is completed after the current point may belong to the front, if at this time current forward will cause the position can not be exchanged to the front, so at this time current does not advance. and the same 1), End backward 1.

Iv. Implementation Code

1, pseudo-code:

Pseudo-code uses a to represent an array of elements

  

Procedure Three-way-partition (A:array of Value, Mid:value):    i←0    j←0    n←size of A-1 while    j≤n:        if A[J] < mid:            swap a[i] and A[j]            i←i + 1            j←j + 1        else if A[J] > Mid:            swap a[j] and a[n ]            n←n-1        Else:            j←j + 1

2. Java Code Implementation:

  

 Public voidSort (list<integer>list) {        intSize =list.size (); intTopend = 0; intBottomstart = size-1; intCurrent = 0;  while(Current <=Bottomstart) {            intCurrentval =List.get (current); if(Currentval < 1) {Swap (list, current, topend); Topend++; Current++; }Else if(Currentval > 1) {Swap (list, current, bottomstart); Bottomstart--; }Else{ Current++; }        }    }

V. Reference documents:

Http://en.wikipedia.org/wiki/Dutch_national_flag_problem

Http://en.wikipedia.org/wiki/Quicksort

Algorithm exercises--the Dutch flag problem

  

Quick sort of deep Dutch flag issue

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.