Get the quicksort Algorithm in one minute

Source: Internet
Author: User
Quicksort is a recursive sort. Algorithm The process of each iteration is to select an element midvalue from the sequence and put all elements smaller than it in front of it. Then we will do the same for the first half and the second half respectively. The Start Element subscript and end element subscript of the child column to be sorted in the start and endlist parameters. In the following implementation, the sorting process is: select the value of the first element as midvalue; border is a subscript variable, meaning that the element on the Right of border is greater than or equal to midvalue, at this time, border = 1; judge all the elements following in sequence. If it is greater than or equal to midvalue, it should be inserted to the right of border, and border + 1; after all the elements are determined, in this case, [midvalue] [values <midvalue] [values> = than midvalue] border-> | border is the subscript of the last element smaller than midvalue. Finally, we can exchange the first element with border. Code

Static   Void Main ( String [] ARGs)
{
// Test1 ();
// List <int> li = new list <int> ();
// Li [18] = 99;
// Test2 ();

// Int [] list = {5, 4, 1, 3, 2, 7, 6 };
// Int [] list = {6, 5 };
// Int [] list = {5, 5 };
// Int [] list = {7, 6, 5, 4, 3, 2, 1 };
// Int [] list = {7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1 };
// Int [] list = {6, 5, 1, 2, 7, 3, 6, 9, 8 };
Int [] List =   New   Int [ 200 ];
Random Rand =   New Random ();
For ( Int I =   0 ; I <   200 ; I ++ )
{
List [I] = Rand. Next ( 120 );
}
Quicksort (list, 0 , List. Length -   1 );

Console. Read ();

}

Private   Static   Void Quicksort ( Int [] List, Int Start, Int End)
{
If (Start > = End) Return ;
Int Midvalue = List [start]; // For performance
Int Border = Start; // The right side of border is the first element greater than or equal to midvalue
For ( Int I = Start +   1 ; I <= End; I ++ )
{
If (List [I] < Midvalue) // To the left of border
{
Swap (list, I, ++ Border );
}
}
Swap (list, border, start );
Quicksort (list, start, border -   1 );
Quicksort (list, border +   1 , End );

}

Private   Static   Void Swap ( Int [] List, Int I, Int J)
{
Int Temp = List [I];
List [I] = List [J];
List [J] = Temp;
}

 

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.