It can be used to solve the problem of sorting problems, which is to arrange N elements into non-decrement order. The method of dividing and rule usually uses the following steps: if n is 1, the algorithm is terminated; otherwise, the set of elements is divided into two or more subset, and each subset is separate, and then sequence sequence The subset is collected and a collection. Suppose only the set of n elements is divided into two subsets. Now you need to determine how to divide the subset. One possibility is to place the front N- 1 element into the first subset (called a), and the last element is placed in the second subset (called B). Sort A in this manner. Since B contains only one element, it has been sorted, and after the serial sequence sequence, only the function I n s E R t in program 2 - 1 0 will be combined with A and B. This sorting algorithm is compared to I N S E R T I O N S O R (see Program 2 - 1 5), which can be found that such a sorting algorithm is actually inserted inserted a recursive algorithm. The complexity of the algorithm is O (N 2). Another way to divide N elements into two subsets is to place an element containing the maximum value into B, and put it in A. Then A is retrofitted. In order to combine the sorted A and B, it is only necessary to add B to a. If the maximum element is found with the function M a x (see programs 1 - 3 1), this sorting algorithm is actually the recursive algorithm of S E L E C T I O N S O R (see Program 2 - 7). If you use the bubbling process (see programs 2 - 8) to find the largest element and move it to the homemost position, this sorting algorithm is the recursive algorithm of B U B B L E S O R (see program 2 - 9). The complexity of these two recursive sort algorithms is (N2). When it is found that A has been discovered, the complexity of the algorithm is O (N2) (see Example 2 - 1 6 and 2 - 1 7). The above segmentation scheme divides N elements into two extreme imbalanced sets A and B. A has N- 1 element, and B contains only one element. Let's take a look at what happen to use the balance segmentation rule: A collection contains N / K elements, and the remaining elements are included. The method of recursively use the division to sort the A and B. Then use a process of being referred to as returning and (M e RG E), and the sequences of the sequences are combined into a set. Example 2-5 Consider 8 elements, the values are [1 0, 4, 6, 3, 8, 2, 5, 7], respectively. If k = 2 is selected, [1 0, 4, 6, 3], and [8, 2, 5, 7] will be independently sorted separately. The results were [3, 4, 6, 1 0] and [2, 5, 7, 8], respectively. Starting from the head of the two sequences and these two sorted sequences. The element 2 ratio 3 is smaller, and the result sequence is moved to the result sequence; 3 and 5 are compared, 3 is transferred into the result sequence; 4 and 5 comparison, 4 is placed in the result sequence; 5 and 6 comparison, .. If k = 4 is selected, the sequence [1 0, 4] and [6, 3, 8, 2, 5, 7] will be sorted. The sorting results are [4, 1 0] and [2, 3, 5, 6, 7, 8], respectively. When the sequence of the two rows is collected, the required sort sequence can be obtained. Figure 2 - 6 gives pseudo code for subsequent sorting algorithms. The number of neutron sets in the algorithm is 2, and the N / K element is contained in A. Template
Void Sort (T E, INT N)
{/ / N elements in E are sorted, k is global variables
IF (n> = k) {
I = N / K; J = N-I;
Let A contain the first i element in E
Let B contain the J Elements of E Summer
S O R t (a, i);
S O R t (b, j);
ME E RGE (A, B, E, I, J,); // Combine A and B into E
}
ELSE uses insertion sort algorithm to sort e
}
Figure 14-6 pseudo code for sorting algorithm
From an algerative and procedure, it is possible to clearly see that the time required to return and n elements is O (n). Set t (n) as a sorting algorithm (as shown in Figure 1 4 - 6), the following recursive formula is required in the worst case,
Where C and D are constants. When N / K≈N-N / K, T (n) is minimized. Therefore, when K = 2, that is, when the number of elements contained in the two subsets is approximately equal, T (N) is minimized, i.e., when the divided subset size is close to, the algorithm of separation is usually optimal.
This recursive method can be calculated by iterative method, resulting in t (n) = (NL O GN). Although this result is obtained when N is 2 power, for all N, this result is also effective because t (n) is N's non-decrement function. T (n) = (NL O GN) gives the best and worst of complexity in the worst case of sorting. Since the complexity of the best and worst case is the same, the average complexity of sorting is t (n) = (NL O GN).
The sorting method of k = 2 in Figure 2 - 6 is referred to as a Multi RGE Sort, or more accurately is a Two-Way Merge Sort. The C function sorted by n-element is written in accordance with the case of k = 2 in Fig. 1 4 - 6 (sorted). One of the simplest ways is to store the element in the linked list (ie, as a member of class C H A I N). In this case, the E is divided into two substantially equal linked list by moving to the n / 2 node and interrupting this chain.
The merged process should be able to generate two sorted layings. If you want to perform the resulting C programs with stacking and insertion sorting, you cannot use a linked list to achieve a sorting, since there is no linked list in the latter two sorting methods. In order to compare the sort function discussed above, the sorting function must store the element set E by an array A, and returns the sequence of elements after A. For this purpose, the pseudo code of Fig. 1 4 - 6 is subjected to the following procedure: When the collection E is divided into two subset, it is not necessary to copy the elements of the two subsets to A and B, and only simple Keep the left and right boundaries of the two subsets in the set E. Next, the initial sequence in A is sorted, and the resulting sequence sequence is collected and in a new array B, and finally copate them into A. Figure 1 4 - 6 Improved version of Figure 1 4 - 7.
Template
M E RGESORT (T A [], int LEFT, INT RIGHT)
{/ / Elements in A [L E f T: R I g H t] are sorted
IF (left INT i = (Left Right) / 2; // Center location M E RGESORT (A, LEFT, I); M E RGESORT (A, I 1, Right); M E RGE (A, B, LEFT, I, Right); // Merge from A to B Copy (B, A, LEFT, RIGHT); / / The result is put back to A } } Figure 14-7 Improvement of the ordering algorithm of separation The performance of Figure 1 4-7 can be improved from many aspects, for example, recursion can be easily eliminated. If the program in Figure 1 4-7 is carefully checked, it will find that the recursive only simply repeat the sequence of sequences until the length of the sequence becomes 1. When the length of the sequence is changed to 1, the return operation can be made, which can be well described with the power of n 2. The sequence having a length of 1 is classified as an ordered sequence of length 2; the length of the length is 2, then the length of the length is followed by an ordered sequence of length 4; this process is constantly repeated until the sequence of the length n. Fig. 1 4 - 8 gives the return (and replication) procedure at N = 8, in square brackets represents the first and end of the sorted sequence. Initial sequence [8] [4] [5] [6] [2] [1] [7] [3] Mergent to B [4 8] [5 6] [1 2] [3 7] Copy to A [4 8] [5 6] [1 2] [3 7] Mergent to B [4 5 6 8] [1 2 3 7] Copy to A [4 5 6 8] [1 2 3 7] Merrgence to B [1 2 3 4 5 6 7 8] Copy to A [1 2 3 4 5 6 7 8] Examples of 200-8 sorting Another two-way merged sorting algorithm is such that the subsequences of each adjacent size is 1 is column, and then the subsequence of the obtained size 2 is adjacent to the subsequence of 2, and then repeated, The merge process is completed until finally a sequence. Culture can be virtually eliminated by caloriling the elements from a to B and from b to A. Separation of the secondary sorting algorithm 1 4 - 3. Procedure 14-3 2nd Road Multiplexing Sort Template Void Mergesort (T A [], INT N) {// Use a merged sort algorithm to sort a [0: N-1] T * b = new t [n]; INT S = 1; // segment size While (s MergePass (A, B, S, N); // Multiple from A to B S = S; MergePass (B, A, S, N); // Multiple from B to A S = S; } } In order to complete the sort code, the function M e rg e p A s s is required. Function M e RG E P A S S (see program 1 4 - 4) is used only to determine the left and right ends of the desired incomplete sequence, the actual mating work is completed by the function M e RG E (see program 1 4 - 5). Function M e RG E requires a user to define an operator <=. If the type of data required is a user-defined type, you must overload the operator <=. This design approach allows us to sort in any of the elements. The purpose of the heavy load operator <= is to compare the domain that needs to be sorted. Program 14-4 MergePass function Template Void MergePass (T x [], T y [], int S, int N) {// Multiple sizes of s INT i = 0; While (i <= n - 2 * s) { // Multiple sizes of two sizes MERGE (X, Y, I, I S-1, I 2 * S-1); i = i 2 * s; } // remain less than 2 elements IF (i s Else for (int J = i; j <= n-1; j ) // copy the last paragraph to Y y [j] = x [j]; } Program 14-5 MERGE function Template Void Merge (T C [], T D [], INT L, INT M, INT R) {// Multiple C [L: M]] and C [M: R] into D [L: R]. INT i = l, // The cursor J = M 1, // of the first paragraph K = L; // / / As long as I and J exist in the segment, constantly return and WHILE ((i <= m) && (j <= r))) IF (c [i] <= c [j]) d [k ] = C [i ]; ELSE D [K ] = C [J ]; // Consider the remaining part IF (i> m) for (int = j; q <= r; q ) D [K ] = C [q]; Else for (int = i; q <= m; q ) D [K ] = C [q]; } Natural Merge Sort is a variation of substantially returning (see programs 1 4 - 3). It first returns to the orderly sequence already existing in the input sequence. For example, elements [4, 8, 3, 7, 1, 5, 6, 2] contain an orderly subsequence [4, 8], [3, 7], [1, 5, 6] and [2 ], These subsequences are generated by scanning the element table from the left to right, and if the element of the element I is larger than the element of the position I 1, segmentation from position I. For the above element sequence, four subsequences, subsequences 1, and subsequences 2 belong to mergers can be obtained [3, 4, 7, 8], subsequence 3 and child sequence 4 to return [1, 2, 5, 6], Finally, the two subsequences are obtained to obtain [1, 2, 3, 4, 5, 6, 7, 7, 8]. Therefore, for the above element sequence, only two is used, and the procedure 1 4 - 3 starts from the subsequence of the size of 1, and three returns must be used. As an extreme example, assuming that the input element sequence has been ranked and there is n elements, natural merged sorting method will accurately identify the sequence does not have to be sorted, but the program 1 4 - 3 still needs [LO G2 N] Return and return. Therefore, natural merges will be sorted within (n). The procedure 1 4-3 will cost (N L O GN) time. Reprinted from Shada Studio