Fast sequencing of three-way division of sorting algorithm

Source: Internet
Author: User

When there are a large number of repeating sort codes in the sequence of elements to be sorted, the efficiency of the simple fast sorting algorithm will be reduced to very low. A direct idea is to divide the pending sequence into three sub-sequences: part of the sorting code is smaller than the base element sort code, and part is equivalent to the base element sort code, which is larger than the base element sort code, as shown in:

However, if we write the implementation algorithm directly on the basis of this idea, we will face great difficulties. How many elements are equivalent to a datum element? And how do you determine the boundaries of the division most quickly and efficiently? Therefore, it is very difficult to complete such a three-way division, even more complex than the two-way division process.
We can realize the three-way division based on the following ideas: In the process of dividing, the elements of the left sub-sequence that are encountered in the scan will be placed at the far left of the sequence, and the elements in the right sub-sequence that are equivalent to the base element sort code are placed at the far right of the sequence. In this way, we will get a sequence diagram as shown below:

When two scan pointers meet, the exact position of the element with the same sort code is known. Then we can get the result of the three-way division by exchanging all the elements of the sort code and the elements that are equivalent to the datum elements and starting with the element that the scan pointer points to.
This method not only effectively deals with the problem of duplicate values in the sequence of elements to be sorted, but also maintains the original performance of the algorithm in the absence of duplicate values. The advantages are:
First, if there are no duplicate values in the sequence of elements, this method can maintain the efficiency of the algorithm because there is no extra work to do;
Second, in the process of each session, you can divide the elements with the same sort code as the Datum elements, and all elements that have the same sort code value will not participate in multiple partitions.
Code

Private void QuickSort(int[] A,intLeftintright) {if(Right <= left)return;/* * Work pointer * p points to the left of the sequence equals the position of the pivot element * Q points to the right of the sequence equal to the position of the pivot element * I points to the element when sweeping from left to right * J points to the right-to-left scan element * /    intP, Q, I, J;intPivot//Anchor Pointi = P = left; j = q = right-1;/ * * Always take the rightmost element of the sequence as anchor * /Pivot = A[right]; while(true) {/* Working pointer I continuously scans from right to left, looking for elements less than or equal to anchor elements */         while(I < right && A[i] <= pivot) {/* Find an element equal to the anchor element to swap it to the position indicated by P */            if(A[i] = = pivot)                {Swap (A, I, p);            p++;        } i++; }/* Working pointer J keeps scanning from left to right, looking for elements greater than or equal to anchor elements */         while(left <= J && A[j] >= pivot) {/* Find an element equal to the anchor element to swap it to the position indicated by Q */            if(A[j] = = pivot)                {Swap (A, J, Q);            q--;        } j--; }/ * * If the two working pointers I j meet, a trip over the end * /        if(I >= J) Break;/* * Swap the element with the left greater than pivot with the right less than the pivot element */Swap (A, I, j);        i++;    j--; }/ * Because the working pointer I points to the next element that currently needs to process the element * you need to return to the actual position of the current element and then swap the equal pivot element to the middle of the sequence * /i--; p--; while(P >= left)        {Swap (A, I, p);        i--;    p--; }/ * Because the working pointer J points to the previous element that currently needs to process the element * you need to return to the actual position of the current element, and then swap the equal pivot element to the middle of the sequence * /j + +; q++; while(q <= right)        {Swap (A, J, Q);        j + +;    q++; }/ * recursively traverse left and right sub-sequences * /QuickSort (A, left, I); QuickSort (A, J, right);}Private void Quick(int[] a) {if(A.length >0) {QuickSort (A,0, A.length-1); }}Private void Swap(int[] arr,intAintb) {inttemp = Arr[a];    Arr[a] = arr[b]; ARR[B] = temp;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Fast sequencing of three-way division of sorting algorithm

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.