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;
}