1,
Quick sorting uses the divide and conquer policy to divide a serial (list) into two sub-serial (sub-lists ).
Steps:
- Picking an element from a series is called a benchmark ),
- Re-sort the series. All elements are placed before the benchmark values smaller than the benchmark values, and all elements are placed behind the benchmark values larger than the benchmark values (the same number can reach either side ). After the partition exits, the benchmark is in the middle of the series. This is calledPartition)Operation.
- Recursively (recursive) sorts the subseries smaller than the reference value element and the subseries larger than the reference value element.
The bottom of recursion is that the number of columns is zero or one, that is, they are always sorted. Although it has been recursive, but thisAlgorithmIt always exits, because in each iteration, it will at least place an element at its final position.
C #CodeAs follows:
View code 1 Public Static Void Sort ( Int [] Numbers)
2 {
3 Sort (numbers, 0 , Numbers. Length - 1 );
4 }
5
6 Private Static Void Sort ( Int [] Numbers, Int Left, Int Right)
7 {
8 If (Left < Right)
9 {
10 Int Middle = Numbers [(left + Right) / 2 ];
11 Int I = Left - 1 ;
12 Int J = Right + 1 ;
13 While ( True )
14 {
15 While (Numbers [ ++ I] < Middle );
16
17 While (Numbers [ -- J] > Middle );
18
19 If (I > = J)
20 Break ;
21
22 Swap (numbers, I, j );
23 }
24
25 Sort (numbers, left, I - 1 );
26 Sort (numbers, J + 1 , Right );
27 }
28 }
29
30 Private Static Void Swap ( Int [] Numbers, Int I, Int J)
31 {
32 Int Number = Numbers [I];
33 Numbers [I] = Numbers [J];
34 Numbers [J] = Number;
35 }
Reference: algorithm Summary