C # implementation of quick sort (using the Randomization principal component method)

Source: Internet
Author: User

The algorithm code is as follows:

/// <Summary>
/// The separation of fast sorting, that is, for a specified Principal Component X, locate the position I so that the left element of I is less than or equal to X, and the right side is greater than or equal to X.
/// </Summary>
/// <Param name = "A"> </param>
/// <Param name = "p"> </param>
/// <Param name = "Q"> </param>
/// <Returns> </returns>
Private int quicksortpartion (INT [] A, int P, int q)
{

Int thecount = Q-p + 1;
// If there is only one element,-1 is returned, indicating no further division.
If (thecount = 1)
{

Return-1;
}
// If the element is 2, you do not need to separate it. You only need to simply process it. This saves some time, but is not required.
If (thecount = 2)
{
If (A [p]> A [Q])
{
Int TMP = A [p];
A [p] = A [Q];
A [Q] = TMP;
}
Return-1;
}
// Obtain the principal element at random.
Random ther = new random (10 );
Int themasterindex = ther. Next (1, thecount );
Int themasterp = P + themasterindex-1;
// Locate the principal component X and exchange it with the lower-bound element.
Int thex = A [themasterp];
A [themasterp] = A [p];
A [p] = thex;
// Search for I so that all elements smaller than or equal to thex and those greater than I are greater than thex. I and P.
Int I = P;
// The initial position of J is I + 1;
For (Int J = p + 1; j <= Q; j ++)
{
// If a [J] is less than or equal to the principal component, the principal component position I shifts one digit to the right, and a [I] is exchanged with a [J ].
If (A [J] <= thex)
{
I ++;
Int thetmp = A [J];
A [J] = A [I];
A [I] = thetmp;
}
}
// Perform an exchange and place the principal component in the position I.
A [p] = A [I];
A [I] = thex;
Return I;
}
/// <Summary>
/// Quick sorting, main function.
/// </Summary>
/// <Param name = "A"> </param>
/// <Param name = "S"> </param>
/// <Param name = "E"> </param>
Private void quicksort (INT [] A, int S, int e)
{
// First find the location I
Int thedivi = quicksortpartion (A, S, e );
// No division is required
If (thedivi <0)
{
Return;
}
Int thep1_s = s, thep1_e = thedivi-1;
Int thep2_s = thedivi + 1, thep2_e = E;
// Recursive quick row on the left
If (thepw.e> = s)
{
Quicksort (A, thep1_s, thep1_e );

}
// Right-side recursive fast sorting.
If (thep2_s <= thep2_e)
{
Quicksort (A, thep2_s, thep2_e );
}
}

The three steps to divide and conquer are: divide, cure, and merge. Fast sorting and Merge Sorting adopt the same sharding policy, and the branch tree of the policy after splitting is 2. However, fast sorting does not require "merging ", after the two branch policies of Merge Sorting are completed, there is also a merge process. Therefore, merging in time complexity is obviously not as fast as sorting. The fast sorting can reach the time complexity θ (N * lg (n), and the merge is θ (N * lg (n )). however, the actual time complexity of the quick rank is related to its implementation and input. In the worst case, the time complexity of the quick rank also reaches θ (N ^ 2, the time complexity of Merge Sorting must be balanced, which is not relevant to the input sequence.

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.