Quick sorting is an improvement of Bubble sorting. Its basic idea is: Split the data to be sorted into two independent parts by means of a lie-down sorting, and all the data in one part is smaller than all the data in the other part, then, the data is sorted by the second method. The whole sorting process can be recursive to convert the entire data into an ordered sequence.
Assume that the array to be sorted is a [1]... A [n], first select a data (usually the first data) as the key data, and then put all the numbers that match it in front of it, all the numbers that are larger than it are placed behind it. This process is called "One lie" and "Fast sorting. The following algorithm is used to sort data quickly:
1) set two variables, I: = 1, J: = N;
2) use the first array element as the key data and assign it to X, that is, X: = A [1];
3) Start from J to search forward, that is, start from the back to search forward (J: = J-1), find the first value less than X, the two exchange;
4) Search backward from I, that is, search backward from the beginning (I: = I + 1), find the first value greater than X, and exchange the two;
5) Repeat steps 3rd and 4 until I = J;
For example, the values of array a to be sorted are: (initial key data X: = 49)
A [1] A [2] a [3] a [4] a [5] a [6] a [7]:
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, and exchange the two. 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 exchange, at this time J: = 4)
At this time, when the third day is not executed, I = J is found, and then 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}
Fast sorting of the first and second parts {13} 27 {38}
End and end 49 {65} 76 {97}
# Include "stdafx. H "# include" stdio. H "Void main () {void quicksort (INT [], Int, INT); int A [7] = {, 5}; int I; quicksort (A,);/* sort the ordered results */for (I = 0; I <7; I ++) printf ("% 4D ", A [I]);} void quicksort (int A [], int left, int right) {int I, j, temp; I = left; j = right; temp = A [left]; If (left> right) return; while (I <j) /* locate the final position */{While (A [J]> = temp & J> I) j --; If (j> I) A [I ++] = A [J]; while (A [I] <= temp & J> I) I ++; If (j> I) A [j --] = A [I];} A [I] = temp; quicksort (A, left, I-1);/* recursion left */quicksort (, I + 1, right);/* Right of recursion */}