Advanced article algorithms, including quick sort and hill sort. The quick sort is introduced first.
1. Quicksort,c.a.r.hoare (1934~) Turing award,1980
2. Divide and conquer strategies
Quicksort VS. MergeSort
(1) Divide the sequence into two sub-sequences: S = S1 + S2 scaled down, independent of each other (max (S1) <= min (S2))
(2) After the sub-sequence is "recursively" sorted, the original sequence is naturally ordered sorted (S) = sorted (S1) + sorted (S2)
(3) Recursive base: Only a single element is left, which is itself the solution
(4) MergeSort's calculation difficulty lies in "close", the difficulty of quicksort lies in "cent"
Okay, let's see quicksort. Specific implementations:
(1) Pivot point
The element on the left is no larger than it, and the right element is no smaller than it.
(2) "Divide" [lo, hi-1] = [lo, mi-1] + [mi] + [mi + 1, hi-1];
The algorithm architecture is as follows
1Template <typename t>2 voidVector<t>:: quicksort (rank lo, rank hi)3 {4 if(Hi-lo <2)//Only one element5 {6 return;7 }8Rank mi = partition (lo, hi-1)//construct Pivot9 quicksort (lo, MI);TenQuicksort (mi+1, HI); One}
Visible, the most important thing is partition this step
Pivot point: (1) may not exist;
(2) The pivot point itself must be in place. In position, the element on the left is no bigger than it, and the element on its right is no smaller than it.
(3) in an ordered sequence, all elements are pivot points, and vice versa
Therefore, a quick sort is the process of converting each element in an array to an axis point one by one
(4) Convert any element to pivot point by proper exchange
Above
Sorting algorithm (Advanced chapter, finishing self-study hall online Deng Junhui teacher "data Structure" course)