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

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

    
    A B C D E
    
    A
    | C D E     - 1 Comparison
    B
    
    A C
    | | E       - 1 Comparison
    B D
    
      A
     / \
    B   C   E   - 1 Comparison
         \
          D
    
    

    E needs 3 comparisons. It should be compared to A, C, D

    Try A-C-D-E in that order.

    Overall there will be nine comparisons -- not very performant.

提交回复
热议问题