Many sorting methods have been discussed before Optimal Sorting. Is there any optimal method? Is there any way to sort the data as quickly as possible? The answer is clearly no. What is the best? A sorting algorithm is affected by many factors. It can also be measured from many perspectives. Some may focus on the number of comparisons, and some may focus on the number of records moving, some of them focus on the use of extra space. Therefore, there is no Optimal Sorting Algorithm, and only the most suitable one. However, sorting algorithms often focus on the number of comparisons in the sorting process. Next we will discuss how to sort by the minimum number of comparisons in comparison-based sorting algorithms. Compare and sort the arrays of N elements. A binary tree can be used to obtain the sequence, as shown in. Two subscripts of the inner node I: j indicates the comparison between Ki and Kj, the left subtree indicates Ki <Kj, the right subtree indicates Ki> Kj, and the leaf node indicates the Base Value of the sorted array, such as a1, a2 ,..., an indicates Ka1 <Ka2 <... & lt; Kan 650) this. width = 650; "id =" nal3 "goog_docs_charindex =" 390 "alt =" "src =" https://docs.google.com/File?id=d556sb3_10gvxrwgfc_b "/> First compare K1 K2. If K1 <K2, go to the left subtree, K2 and K3 to compare. If K2> K3, go to the right subtree and compare K1 K3, K1 <K3, then we know that K1 <K3 <K2. We found that was not compared after, because K1 <K2 and K2 <K3, we already know K1 <K3, so there is no need to compare K1 K3, this is a redundant situation, so such a path will never be accessible, and we are concerned with the minimum number of comparisons, so this situation is eliminated directly. In this way, the inner node of a binary tree represents the comparison method for the final ordered sequence under all possible input conditions. After removing redundancy, the nodes sort the arrays of n elements, the number of leaf nodes in a binary tree is n !, That is, all the permutation and combination of n elements. The next question is, in all possible binary sorting trees, what is the minimum value in the maximum number of comparisons per tree? This minimum value is the minimum number of comparisons in the worst case of sorting n elements. Assume that the minimum value is S (n). If the number of layers in the inner node of this sort binary tree is <K, it is clear that the tree has a maximum of 2 K leaf nodes. N! <= 2 s (n) Because S (n) is an integer, S (n)> = log2n! The upper limit. In the preceding algorithm, the minimum number of comparisons in the worst case is binary insertion, tree selection, two-way merging, and binary insertion. The number of comparisons is 650) this. width = 650; "id =" q6qn "goog_docs_charindex =" 1000 "alt =" "src =" https://docs.google.com/File?id=d556sb3_11hntphddb_b "/> When n is not very large, we can calculate the number of comparisons as follows: 650) this. width = 650; "id =" zu6s "goog_docs_charindex =" 1041 "alt =" "src =" https://docs.google.com/File?id=d556sb3_12hrdx78g4_b "/> For S (4), the final result can be obtained by five comparisons. For S (5), the result may be 7 or 8, and the result is sorted by five numbers, how can we get the result through 7 comparisons? First, compare K1 K2, then compare the maximum values of K3 K4, and compare them three times in total. Then we can get the following figure: 650) this. width = 650; "id =" m "goog_docs_charindex =" 1167 "alt =" "src =" https://docs.google.com/File?id=d556sb3_13f2fnc5f8_b "/> The Arrow between a and B indicates a <B. Then we insert e into a B d. The following four situations may occur: 650) this. width = 650; "id =" c3cf "goog_docs_charindex =" 1214 "alt =" "src =" https://docs.google.com/File?id=d556sb3_14gfz6bhhn_b "/> Compare two times at most. e is compared with B first. If it is large, it is compared with d. If it is small, it is compared with.
After inserting c into a B d e, we can find that we only need to compare it twice at most. If it is small, it will be compared with a. Otherwise, it will be compared with B, c and d have already been compared. So the total number of times is 3 + 2 + 2 = 7. So what if we promote this method to generalization? Let's call it the merge insertion method. Let's take the first 10 pairs of elements in the example of 21 Elements, K1: K2 K3: K4... k19. width = 650; "id =" a28h "alt =" "src =" https://docs.google.com/File?id=d556sb3_15d7bgczdx_b "/> Insert b3 to b1 a1 a2. As discussed above, it takes two times to insert b2, which is also two times. The following sequence can be obtained: 650) this. width = 650; "id =" tbbb "alt =" "src =" https://docs.google.com/File?id=d556sb3_16dbxb6dd6_b "/> We call the element referred to by the arrow at the top of the list the main chain, that is, the sorted element. Then we can insert b5 into the main chain for three comparisons, first, compare it with c4. If it is small, compare it with c2. If it is small, compare it with c1. Otherwise, compare it with c3. If it is bigger than c4, it is bigger than c6, and then compare it with a4, otherwise, we can insert b4 into the main chain using three Comparisons similar to c5. Expected result: 650) this. width = 650; "id =" ey "alt =" "src =" https://docs.google.com/File?id=d556sb3_17fktpbzd5_b "/> Next is the most critical step. We inserted b11 into the main chain instead of b6. We compared it with d8 for four times, and then compared it with d4 or a7, then compare it with d2, d6, d10, or a9, and then compare it again, four times in total. Next, insert b10 b9 b8 b7 b6 into the sequence, each of which requires four times, therefore, the total number of comparisons is 20 + 22 + 2 + 2 + 3 + 3 + 4 + 4 + 4 + 4 + 4 + 4 = 66 because 265 <21! <266 because S (21) = 66, we sort the 21 numbers at least 66 times. Why do we need to insert b11 first? We can find that there are 15 elements in the main chain, and a proper insertion position can be found through the binary method for four comparisons. However, if the elements between b6 and b10 are inserted first, then there are 16 main chain elements. The worst case is 5 times before b11 can be inserted. So why insert b10 instead of b6 first? After b11 is inserted into the main chain, the number of main chains is 16, but b10 and a10 have already been compared. Therefore, you only need to find a proper position in the first 15 elements four times, if b6 is inserted first, there are 10 elements in front of b6, and the worst is four times. Even if the last five elements are inserted before a6, there are only 15 elements, it only takes four times, so the insertion order is b11 b10... b6 to minimize the total number of comparisons in the worst case. After finding out the most important part, we can start to promote n. The following is the description of the merge insertion Algorithm. 1. Compare n elements with one to the other, find out the maximum number of each pair. If n is an even number, there is no remaining element.) 2. Sort the number of 2/n using merge insertion. 3. name these elements a1, respectively, a2 ,..., a2/n b1, b2 ,..., b2/n if n is an odd number, B (2/n + 1) Where a1 <a2 <... <a2/n ai <bib1 and a are collectively referred to as the main chain. The next task is to insert the other B into the main chain using the binary insertion method. Insert order of remaining elements: 650) this. width = 650; "id =" fqpf "alt =" "src =" https://docs.google.com/File?id=d556sb3_18g9v3hmcj_b "/> The next task is to define (t1, t2, t3, t4 ,...) = (1, 3, 5, 11 ,...) such a sequence can follow 650) this. width = 650; "id =" jbqx "alt =" "src =" https://docs.google.com/File?id=d556sb3_19hdg4n6cn_b "/> In the order of k comparisons, these elements are inserted into the main chain, as shown in: 650) this. width = 650; "id =" sib40 "alt =" "src =" https://docs.google.com/File?id=d556sb3_20hjj37dfx_b "/> We know that the total number of nodes in the main chain containing nodes At (k-1) is: 2Tk-1 + (Tk-Tk-1-1) = Tk-1 + Tk-1 this number must be less than 2 k, preferably equal to 2 k -1 both: Tk-1 + Tk = 2 k T1 = 1 Let's assume T0 = 1 so that we can obtain the value of this sequence. Conclusion: This is what I saw when I read The Art of Computer Programming last night. After I understood it for a long time, I got into The door. The analysis later was omitted, if you are interested, you can find books and read them. The algorithm is really clever and thoroughly analyzed, no one will study how to minimize the number of comparisons in the sorting process to save a few comparisons. The most important thing is to use this algorithm to understand how the author analyzes the problem, promote the problem and finally solve the problem.