Design an efficient algorithm to sort 5 distinct keys in fewer than 8 comparisons

前端 未结 13 1369
轮回少年
轮回少年 2020-12-01 11:30

Design an efficient algorithm to sort 5 distinct - very large - keys less than 8 comparisons in the worst case. You can\'t use radix sort.

13条回答
  •  一向
    一向 (楼主)
    2020-12-01 11:51

    Compare A to B and C to D. WLOG, suppose A>B and C>D. Compare A to C. WLOG, suppose A>C. Sort E into A-C-D. This can be done with two comparisons. Sort B into {E,C,D}. This can be done with two comparisons, for a total of seven.

提交回复
热议问题