High-performance algorithms are often required in the design for code optimization. The following describes a fast sorting method with source code.
Quick sorting(Quicksort) is an improvement on Bubble sorting. Proposed by C. A. R. Hoare in 1962. 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 recursive to convert the entire data into an ordered sequence.
PrincipleSet the array to be sorted to A [0]... A [N-1], first randomly select A data (usually the first data) 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 is called a fast sorting. The quick sorting algorithm is as follows:
1) set two variables I, J, sorting start: I = 0, J = N-1;
2) Take the first array element as the key data and assign it to the key, that is, key = A [0];
3) search forward from J, that is, search forward from the back (J = J-1), find the first value less than the key a [j], and exchange with the key;
4) Search backward from I, that is, search backward from the beginning (I = I + 1), find the first a [I] greater than the key, and exchange with the key;
5) Repeat steps 3rd, 4, 5 until I = J; (Step 4 is not found in the program when j = J-1, I = I + 1. When I is found and switched, the position of the j pointer remains unchanged. In addition, when I = j, the process must be exactly the end of the last loop completed by I + or j +)
For example, the values of array A to be sorted are: (initial key data: X = 49) Note that key X remains unchanged. it will always be compared with X. No matter what seats, the final goal is to put X in the middle and small in front and big in the back.
A [0], a [1], a [2], a [3], a [4], a [5], and a [6]:
49 38 65 97 76 13 27
After the first exchange: 27 38 65 97 76 13 49
(Start from the end of step 3 of the algorithm)
After the second exchange: 27 38 49 97 76 13 65
(According to the fourth step of the algorithm, find the value> X from the beginning, 65> 49, the two are exchanged, at this time: I = 3)
After the third exchange: 27 38 13 97 76 49 65
(Follow the fifth step of the algorithm to find the third step for executing the algorithm again.
After the fourth exchange: 27 38 13 49 76 97 65
(According to the fourth step of the algorithm, find the value greater than X from the beginning, 97> 49, the two are exchanged, at this time: J = 4)
At this time, we will find that I = J and end the fast sorting. After a fast sorting, the result is: 27 38 13 49 76 97 65, that is to say, all the numbers greater than 49 are behind 49, so all the numbers smaller than 49 are above 49.
Fast sorting is a recursive call to this process-split the data sequence with 49 as the midpoint and perform similar fast sorting on the previous and subsequent parts respectively to complete the fast sorting of all data sequences, finally, the data sequence is converted into an ordered sequence. According to this idea, the entire process of fast sorting for the preceding array a is shown in 6:
Initial status {49 38 65 97 76 13 27}
After a quick sorting, it is divided into {27 38 13} 49 {76 97 65}
Sort the first and second parts respectively. {27 38 13} after switching between step 3 and Step 4, the parts are sorted by {13 27 38.
{76 97 65} after switching between step 3 and Step 4, it is changed to {65 76 97.
Code
Using System;
Using System. Text;
Namespace QuickSortArithmetic
{
Class Program
{
Static void Main (string [] args)
{
// Generate an int32 array of 70 three-digit numbers as needed
Int [] Nums = RandomNums (3, 70 );
ShowNums ("initialized array:", Nums );
QuickSort (ref Nums, 0, Nums. Length-1 );
ShowNums ("sorted array:", Nums );
Console. ReadLine ();
}
# Region generate a random int Array
Private static int [] RandomNums (int Length, int Count)
{
Random Rand = new Random (7 );
Int [] Nums = new int [Count];
For (int c = 0; c <Count; c ++)
{
String Result = string. Empty;
For (int I = 0; I <Length; I ++)
{
Result + = Rand. Next (10). ToString ();
} Nums [c] = Convert. ToInt32 (Result );
} Return Nums;
}
# Endregion
# Region Sorting Algorithm: Fast sorting
Private static void QuickSort (ref int [] Nums, int low, int high)
{
Int key = Nums [low];
Int I = low;
Int j = high;
While (low {
While (low {
If (Nums [high] <key)
{
Nums [low] = Nums [high];
Nums [high] = key;
Break;
}
Else
{
High --;
}
}
While (low {
If (Nums [low]> key)
{
Nums [High] = Nums [low];
Nums [low] = key;
Break;
}
Else
{
Low ++;
}
}
}
If (I <low-1)
{
Quicksort (ref Nums, I, low-1 );
}
If (j> high + 1)
{
Quicksort (ref Nums, high + 1, J );
}
}
# Endregion
Private Static void shownums (string msgs, int [] Nums)
{
Console. Write (MSGs );
Foreach (int n in Nums)
{
Console. Write (N. tostring () + "");
}
Console. writeline ();
}
}
}
Sorting result: