This preview shows page 1. Sign up to view the full content.
51
Best case: 2 compares.
Avgcase:16/6=22/3compares
.
Worst case: 3 compares.
(b)
Doing a similar approach of building a decision tree for 5 numbers
is somewhat overwhelming since there are 120 permutations. A pretty
good algorithm can be had be building on Sort3 from part (a). Use Sort3
to sort the
f
rst 3 numbers. Then, add the 4th number in 2 comparisons
by checking the middle of the
f
rst 3, and then checking the 1st or 3rd as
appropriate. The last number can be added using at most 3 comparisons
by checking the 2nd of the
f
rst 4 numbers, then (at worst) the 3rd and
4th. Thus, the total number of comparisons is at most 8. The best case
is 6, the average case is 7 4/15 (2 2/3 for the
f
rst 3 numbers, exactly 2
for the 4th number and 2 3/5 for the 5th number).
It is possible to do this in 7 comparisons, worst case. Seek Knuth,
Volume 3.
(c)
Call the algorithm from part (b) Sort5. Use it to sort the
f
rst 5 num
bers in at most 8 comparisons. Now, add in the sixth number by
f
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '08
 BELL,D

Click to edit the document details