This article describes a method for sorting 5 numbers, using only 7 comparisons. Suppose the number to be sorted is a,b,c,d,e.
First, A, B is compared , assuming the result is a<b, and then compare c,d, assuming the result is c<d; and then compare the larger of the two groups (that is, compare b,d), assuming the result is b<d, So there is the following relationship, the relationship between the arrows means "<", that is "less than", so far, has been three times compared .
, the meaning of this figure is: A<b<d,c<d
Now insert e into the appropriate position in {A,b,d}, and when looking for a location using the binary lookup method, only two comparisons are required--compare with B first, and then compare with a or D. When e is inserted into {a,b,d}, there are four results in the following:
In these four cases, a maximum of two comparisons are required to insert C into a sequence composed of [ABCD]. Also use the dichotomy method to find the insertion position. Take the first case: C compares with a first, if it is greater than a, and then compared with B, if it is greater than B, it will not be compared with D , because we have known c<d before.
Reference: Computer programming art, Volume III, page:174
Complete
A method of 7 comparisons for sorting 5 numbers