[Code] For an array with 5 elements, at least several comparisons are required to ensure that the array is in order?

problem

For an array with 5 elements, at least several comparisons are required to ensure the array is in order?
A. 5 times
B. 6 times
C. 7 times
D. 8 times

answer

C

lemma

Two comparisons can insert an unknown element into an ordinal sequence of 3 elements and ensure that the sequence is in order after the insertion is completed.

Proof of Lemma

Suppose the unknown number is x, and the original number sequence is a, b, c, and a<b<c, first compare x and b, and set x<b, c<d. Then compare the size of b and d. Let's set b<d, and then compare the size of x with a. If x is small, then x<a<b<c, otherwise a<x<b<c; if x>b, then The comparison between x and c is the same as the former. The lemma is proved.

Problem analysis

Going back to the original question, we might as well set these 5 numbers as a, b, c, d, and e. First, compare the sizes of a and b, and compare the sizes of c and d, and set a<b, c<d. Then compare the sizes of b and d, and set b<d. Then, get an ordered sequence of length 3 a<b<d, and know that c<d. According to the lemma, insert e into a, b, d in this series. Since c<d is known, then c can only determine the relative size with the three numbers a, b, and e at most (if e>d, then c<d<e, the relative size of c and e has also been determined), and a The relative magnitudes of the three numbers, b, and e have been determined. Let us set a<b<e. According to the lemma, we can use two comparisons to insert c into the sequence of a, b, and e. Knowing that a, b, c, and e are all smaller than d, so the 5 numbers are sorted, and we have a total of 7 comparisons.

Introduce decision tree

The concept of decision tree is introduced below to explain why it is impossible for 6 times or less.

Decision trees are generally generated from top to bottom. Each decision or time (that is, the natural state) may lead to two or more events, leading to different results. Drawing this kind of decision branch into a graph like the branches of a tree, so it is called a decision tree.

For sorting events, each different state is a set of comparison results such as a<b, if it is true, it will trigger some results, if it is false, it will trigger other results, and the final result is these 5 elements It is easy to know that there are 5!=120 kinds of size relationships, so there are a total of 120 leaf nodes in the decision tree.

Obviously, each state in the decision tree has only two possibilities of true and false, so this decision tree is a binary tree, and the depth of a decision tree determines how many times this sorting comparison method must be compared at least to ensure that this is determined. The order of 5 trees. Because 120>2 6 , the number of comparisons with a depth of 6 or less cannot guarantee the determination of the size relationship of 5 numbers.

In summary, 5 elements need at least 7 times to ensure that the elements can be sorted.

inference

A more general question: For any N, at least how many times does it take to guarantee the sorting order? Regrettably, there is no clear answer to this question so far, but based on the above analysis of the decision tree, an obvious fact is. Whether the lower limit of the number of comparisons can be reached for every N is still unknown. For more detailed and in-depth discussion, please refer to Donald E. Knuth's famous book " The Art of Computer Programming " ( The Art of Computer Programming ) Volume 3, Chapter 5, Section 3, "Optimal Training · Minimal Comparison Sorting" part .

However, we can derive an obvious inference from this, that is, the lower limit of the time complexity of the comparison-based sorting algorithm is O(NlogN). The calculation of asymptotic time complexity ignores low-order functions, so [NlogN]-N=O(NlogN).

Guess you like

Origin blog.csdn.net/Ljnoit/article/details/108985635