The goal was to produce decision trees, of the minimum possible depth, to sort four (and then five) objects using only comparisons of two objects. I put the decision trees for two and three objects on the board:
For n=2: Test a:b, output "ab" or "ba" accordingly.
For n=3: Test a:b. If a is first, test b:c and output "abc" if b is first. Otherwise test a:c, and output "acb" or "cab" accordingly. If the initial test has b first, switch a and b.
The Sorting Lower Bound Theorem says that we cannot do better than depth 5 for n=4, or better than depth 7 for n=5.
Solution:
For n=4 there are several possible solutions. One of the simplest is to
use up to three comparisons to find the order of a, b, and c, then use two
more to place d in one of four places relative to these three. For example,
in place of the "abc" leaf of the depth-3 tree, test b:d. If b is first test
c:d and output "abcd" or "abdc", if d is first test a:d and output "adbc" or
"dabc".
Another approach to n=4 mimics Mergesort. Test a:b and c:d, then use the
last three comparisons to merge the two sorted lists. If a<b and c<d
this works as follows: Test a:c. If a is first test b:c, then either output
"abcd" or use one test to decide between "acbd" and "acdb". If c is first,
the decisions are the same except that you must switch a with c and b with d.
These first three comparisons -- a:b, c:d, and winner against winner, are
also a good idea with n=5 because each of the eight possible outcomes of the
three tests corresponds to 15 of the 120 possible permutations of a, b, c, d,
and e. (Each gives you three of the 24 permutations of a, b, c, and d, and
for each of these there are five places to put e. Below we will solve the case
where a<b, c<d, and a<c. The other seven of the eight outcomes of
the first three tests are symmetrical. If b<a we switch a with b, if
d<c we switch c and d, and if the third test goes differently we switch
the two winners and the two losers of the first two tests.
For the fourth test with n=5, we need to separate the fifteen permutations
still possible into a set of seven and a set of eight. By trial and error,
we find that e:c is the test to make:
We have three comparisons left to decide among each set of alternatives.
In the first set, we next test b:c. If b<c, testing b:e gives b<e
in the case "abecd" and e<b in the cases "eabcd" and "aebcd". If c<b,
testing a:e splits the four possibilities into two groups of two. And with
two alternatives, we can always decide with one more comparison.
This leaves us with just the second set of alternatives above. Testing
d:e leaves us in one of two situations:
Last modified 12 April 2006