The method of separation and treatment can also be used to achieve another completely different sorting method, which is referred to as a Quick Sort. In this method, n elements are divided into three segments: the left segment L e f T, the right segment R I g H T and the mid-section M i D D L e. Middle section only contains an element. Each element in the left segment is less than or equal to the mid-section element, and each element in the right segment is greater than or equal to the mid-stage element. Therefore, the elements in L E f T and R I g H t can be sorted independently, and the ordering results of L E f T and R I g H t are not required to be combined. Elements in M i D D L e are referred to as fulcrum (P i V O T). A quick sorted pseudo code is given in Figure 1 4 - 9. / / Use a quick sort method to select an element from a [0: N- 1] from a [0: N- 1] as a middle, which is divided into two segments of LEFT and Right, making left The elements in the middle are equal to the fulcrum, and the elements in Right are greater than the rapid sorting method to reciprocally use the fast sorting method to use the fast sorting method to use the fast sorting method for the Right Sorting method for the Right MIDDLE RIGHT Figure 14-9 Fast Sorted pseudo code examines element sequence [4, 8, 3, 7, 1, 5, 6, 2]. Assume that the element 6 is selected as the fulcrum, then 6 is located at L E f T at L E f T at the L E f T at the M i D1; 8, 7 is located in R I g H t. When the LEFT is rated, the resulting result is 1, 2, 3, 4, 5; after the R I g H T is sequence, the resulting result is 7, 8. After placing the elements in Right, the elements in the l e f t are placed before the fulcrum element [1, 2, 3, 4, 5, 6, 7, 8]. The element sequence is divided into L E f T, M i D1 and R I g H T can be carried out in place (see program 1 4 - 6). In program 1 4 - 6, the fulcrum is always taken in position 1. Other options can also be used to improve sort performance, which will give such a selection later. Program 14-6 Quick Sort Template
Void Quicksort (T * a, int N)
{// quick sorting of A [0: N-1]
{// Requirement a [N] must have a maximum key value
Quicksort (A, 0, N-1);
Template
Void Quicksort (T A [], INT L, INT R)
{// Sort A [L: R], A [R 1] has a large value
IF (l> = r) return;
INT i = L, // from left to right
J = R 1; // From right to left cursor
T pivot = a [l];
/ / Exchange the elements of the left> = pivot with the elements of the right side <= pivot
While (true) {
Do {// Look on the left> = Elements of Pivot
i = i 1;
WHILE (a [i] Do {// Look at the elements of <= pivot on the right J = J - 1; } while (a [j]> pivot); IF (i> = j) Break; // No exchange object SWAP (a [i], a [j]); } // Set P i v o T a [l] = a [j]; a [j] = pivot; Quicksort (A, L, J-1); // Sort Quicksort (A, J 1, R) on the left segment; // Sort the right segment } If the program 1 4 - 6 in D O - W H i L E is modified to <= and> =, the program 1 4 - 6 is still correct. The experimental results show that the rapid sorting code of the program 1 4 - 6 can get a better average performance. In order to eliminate recursive in the program, the stack must be introduced. However, eliminating the last recursive call does not need to use the stack. Eliminate the work of recursive calls (Exercise 1 3). The recursive stack space required for program 1 4 - 6 is O (n). If you use a stack to simulate recursive, this space can be reduced to O (L O GN). In the simulation process, the smaller people in LEFT and RIGHT are first sorted, and the boundary of the larger is placed in the stack. In the worst case, L E f T is always empty, and the calculation time required for rapid sorting is (N2). In the best case, the number of elements in L E f T and R I g H t is substantially the same, and the complexity of rapid sorting is (NL O GN). Surprisingly, the average complexity of rapid sorting is also (NL O GN). The average complexity of the pose 2-1 is (NL O GN). It is proved that the average time of sorting the array containing N elements with T (N). When n≤1, t (n) ≤ D, D is a constant. When n <1, s represents the number of elements contained in the left segment with s. Since there is a fulcrum element in the middle section, the number of elements in the right segment is N-S- 1. Therefore, the average sorting time of the left and right sections is T (s), T (N-S-1). The time required for the elements in the segmentation array is represented by CN, where C is a constant. Because S has an equal opportunity to take any of 0 to n-1. If N is used in (2-8), T (N) ≤ Kn LO GE N, wherein N> 1 and K = 2 (C D), E ~ 2. 7 1 8 are natural The logarithmic substrate. The correctness of the formula when n = 2 is first verified at the beginning of the start. According to the formula (1 4 - 8), T (2) ≤ 2c 2d ≤ K NL O GE 2 can be obtained. In summarizing the hypothesis part, it is assumed that t (n) ≤ Kn L O GE N (when 2 ≤ N Figure 1 4 - 1 0 compares the algorithms discussed in this book under average conditions and the complexity under the worst conditions. Methods of the worst complexity average complexity Bubbling N2 N2 Count sort N2 N2 Insert sort N2 N2 Select sort N2 N2 Pile Sort NL O GN NL O GN Multiple sort NL O GN NL O GN Rapid sort N2 NL O GN Figure 14-10 Comparison of various sort algorithms Median-of-Three Quick Sort is a change in programs 1 4 - 6, which has better average performance. Note that A [1] is always selected as a fulcrum in the program 1 4 - 6, and in this fast sorting algorithm, it is not necessary to use A [1] as a fulcrum, but to take {a [1], A [(1 r) / 2], the element in the size of the size in a [r]} is used as the fulcrum. For example, if there are three elements, the size is 5, 9, 7, respectively, and 7 is the fulcrum. In order to achieve a medium-value rapid sorting algorithm, a simplest way is to first select a median element and exchange with a [1], then use program 1 4 - 6 to complete sorting. If A [R] is the selected median element, the A [1] is exchanged with a [R], and then a [1] (ie the original A [R]) assigns to the program 1 4 - 6 The variable pivot is then continued to perform the rest of the program 1 4 - 6. Figure 2-111 shows the average time of return, stack sorting, insertion sorting, and rapid sorting, rapid sorting, rapid sorting, and rapid sorting according to experimentation. For each different N, at least 1 0 0 groups are randomly generated. The production of random integers is achieved by repeated calling S t dl i b. The R A n d O m function in the H library is implemented. If you sort a set of integers less than 1 0 clock, you will continue to sort other groups until the time used is not less than 1 0 clock. The data in Figure 2-11 contains time to generate random integers. For each N, the time used to generate random integers and other overhead in various sorting methods. Therefore, the data in Figures 2 - 11 is useful for comparing various sort algorithms. For sufficient N, the rapid sorting algorithm is higher than other algorithms. From the figure, it can be seen that the intersection of the rapid sorting curve and the insertion sorting curve is slightly smaller than 2 0, and the exact value of this intersection absorbing can be determined by experiment. Experiments can be carried out by n = 1 5, 1, 1 7, 1, 1, 1, respectively to find accurate intersection. Preferably, the intersection is the NBR E A K. When n ≤ nbreak, the average performance of insertion sorting is best. When n> NBREAK, rapid ordering performance is best. When N> Nbreak, the insertion sorting and rapid sorting is combined into a sort function, and the performance of rapid sorting can be improved, and the implementation method is to put the following statement in program 1 4 - 6: IF (l> = r) R e t U R n; Replace IF (r-1 Here, I N S E R TRI NS OR (A, L, R) is used to insert the A [1: R]. Measure the performance of the modified rapid sorting algorithm to leave exercises (exercise 2 0). Replacing N b R E a K with smaller values may further increase performance (see Exercise 2 0). Most experiments show that when N> C (C is a constant), the performance of the sorting in worst case is also the best. When N ≤ C, in the worst case, insertion is optimal. By mixing the insertion sorting with the merged, the performance of the sorting can be improved (Exercise 2 1). Reprinted from Shada Studio