The basic idea of quick sorting is:
By a lying sort of data to be sorted into separate two parts, one part of all the data is smaller than the other part of all the data, and then the second method of the two parts of the data are quickly sorted, the entire sorting process can be recursive , so as to achieve the entire data into an ordered sequence.
Suppose the array to be sorted is a[0] ... A[n-1], the first arbitrary selection of data (usually the first data) as the key data (key), and then put all the number than it in front of it, all larger than its number is placed behind it, this process is called a lie fast sort. A quick-sort algorithm for lying down is:
1), set two variables I, J, at the beginning of the sorting i:=1,j:= N;
2) with the first array element as the key data, assign the value to X, i.e. x:=a[0];
3), starting from J forward search, that is, after the start of the forward search (j:=j-1), find the first value less than X, the two exchange;
4), from I start backward search, that is, to start backward search (i:=i+1), find the first value greater than X, the two exchange;
5), repeat the 3rd and 4 steps until i=j (while i = = j);
For example: The value of array A to be sorted is: (initial critical data x:=49)
A[0] a[1] a[2] a[3] a[4 a[5] a[6]:
49 38 65 97 76 13 27
After the first exchange: 27, 38, 65, 97, 76, 13, 49 (follow the third step of the algorithm from the back to find
After the second exchange: 27, 38, 49, 97, 76, 13, 65 (follow the fourth step of the algorithm from the front to find the value of >x, 65>49, both exchange, at this time i:=3)
After the third exchange: 27, 38, 13, 97, 76, 49, 65 (the third step in the algorithm is to execute the algorithm in the fifth step from the beginning to find
After the fourth exchange: 27, 38, 13, 49, 76, 97, 65 (follow the fourth step of the algorithm from the front to find values greater than X, 97>49, both exchanged, at this time j:=4)
At this point in the third step of the time to find i=j, so that the end of a quick sort, then after lying in a quick sort after the result is: 27, 38, 13, 49, 76, 97, 65, that is, so the number greater than 49 is all behind 49, so the number of less than 49 is all in front of 49.
[CPP]View PlainCopy
- #include "stdafx.h"
- #include <stdio.h>
- void Quick_sort (int s[], int L, int R)
- {
- int I, j, X;
- if (L < R)
- {
- i = l;
- j = r;
- x = S[i];
- While (i < J)
- {
- While (i < J && S[j] > x)
- j--; / * Find the first number less than x from right to left * /
- if (i < j)
- s[i++] = S[j];
- While (i < J && S[i] < x)
- i++; / * Find the first number greater than x from left to right * /
- if (i < j)
- s[j--] = s[i];
- }
- S[i] = x;
- Quick_sort (S, l, i-1); / * Recursive call * /
- Quick_sort (S, i+1, R);
- }
- }
- int _tmain (int argc, _tchar* argv[])
- {
- int a[]={49,38,65,97,76,13,27};
- int l = 0;
- int r = 6;
- Quick_sort (A,L,R);
- For (int i=0;i<=r;i++)
- printf ("%4d", A[i]);
- return 0;
- }
Operation Result:
Fast sequencing of C language source code and analysis