Quick sorting
The most efficient sorting algorithm.
It adopts the idea of divide and conquer: first ensure that the first half of the list is smaller than the second half, and then sort the first half and the second half separately, so that the whole list is ordered. This is an advanced idea and the reason for its efficiency. In sorting algorithms, the efficiency of algorithms is directly related to the number of comparisons in the list, the "make sure that the first half of the list is smaller than the second half" means that the first half of the list is no longer compared with the second half, this greatly reduces unnecessary comparisons between numbers.
QuickSort is an improvement in Bubble sorting. Its basic idea is: Split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be as follows:To convert the entire data into an ordered sequence.
Set the array to be sorted to A [0]... A [N-1], first randomly select A data (usually the first number of the array) as the key data, and then put all the smaller than it before it, all the numbers larger than it are placed behind it. This process becomes
If I and j don't meet each other, I will find the bigger one. If not, I will decrease the number of j's smaller ones. This repeats and repeats. Note that both judgment and search are performed at the same time.
Note:Quick sorting does not directly obtain the final result, but only splits the number greater than k and smaller than k into the two sides of k. (You can imagine that I and j are two robots, and the data is different sizes of stones. First, take the stones in front of I and set aside space for maneuver, then they took turns to pick stones larger than k and smaller than k and threw them to the opposite side. Finally, they put the stone they took back, so I threw all the stones bigger than this one to the j side, and all the small ones to the I side. It's just a good luck this time. After throwing it all, it's just neatly arranged .) To get the final result, you need to perform this step on the arrays on both sides of subscript 2 again, and then break down the array until the array can no longer be decomposed (only one data) to get the correct result.
Namespace QuickSort
{
Class Program
{
Static void Main (string [] args)
{
//
Int [] array = {49, 38, 65, 97, 76, 13, 27 };
Sort (array, 0, array. Length-1 );
Console. WriteLine (string. Join (",", array ));
Console. ReadLine ();
}
/// <Summary>
/// A sorting unit. This method is completed. The left side of the key is smaller than the key, and the right side of the key is larger than the key.
/// </Summary>
/// <Param name = "array"> sort array </param>
/// <Param name = "low"> Start position of sorting </param>
/// <Param name = "high"> sorting end position </param>
/// <Returns> array after cell sorting </returns>
Private static int sortUnit (int [] array, int low, int high)
{
Int key = array [low];
While (low
{// Search from the back and forward for a value smaller than the key
While (array [high]> = key & high> low)
-- High; // left if it is smaller than the key
Array [low] = array [high]; // search for a value greater than the key from the front to the back, and place the value greater than the key on the right.
While (array [low] <= key & high> low)
+ + Low; // greater than the key, put it on the right
Array [high] = array [low];
} // The left side is smaller than the key and the right side is larger than the key. // Place the key at the current position of the cursor. // At this time, low is equal to high
Array [low] = key;
// Console. WriteLine (string. Join (",", array ));
Return high;
}
/// <Summary>
/// Quick sorting
/// </Summary>
/// <Param name = "array"> </param>
/// <Param name = "low"> </param>
/// <Param name = "high"> </param>
Public static void sort (int [] array, int low, int high)
{
If (low> = high)
{
Return;
}
Else
{
Int index = sortUnit (array, low, high); // completes unit sorting
Sort (array, low, index-1); // sort the units on the left
Sort (array, index + 1, high); // sort the right cell
}
}
}
}