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

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

    Sample sequence of operations, using mergesort (the merge function below will merge two sorted sublists into a single sorted combined list):

    elements[1..2] <- merge(elements[1..1], elements[2..2]) # 1 comparison
    elements[3..4] <- merge(elements[3..3], elements[4..4]) # 1 comparison
    elements[3..5] <- merge(elements[3..4], elements[5..5]) # 1-2 comparisons
    elements[1..5] <- merge(elements[1..2], elements[3..5]) # 2-4 comparisons
    

提交回复
热议问题