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

前端 未结 13 1364
轮回少年
轮回少年 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:53

    It has to be 7 or more comparisons.

    There are 120 (5 factorial) ways for 5 objects to be arranged. An algorithm using 6 comparisons can only tell apart 2^6 = 64 different initial arrangements, so algorithms using 6 or less comparisons cannot sort all possible inputs.

    There may be a way to sort using only 7 comparisons. If you only want to sort 5 elements, such an algorithm could be found (or proved not to exist) by brute force.

提交回复
热议问题