algorithm - How to sort a 0/1 array with 2n/3 comparisons?

前端 未结 2 1171
感动是毒
感动是毒 2021-02-12 21:32

In Algorithm Design Manual, there is such an excise

4-26 Consider the problem of sorting a sequence of n 0’s and 1’s using comparisons. For each compari

2条回答
  •  囚心锁ツ
    2021-02-12 21:57

    "0 or 1 with equal probability" is the condition for "average" case. Other cases may have worse timing.

    Hint 1: 2/3 = 1/2 + 1/8 + 1/32 + 1/128 + ...

    Hint 2: Consider the sequence as a sequence of pairs and compare the items in each pair. Half will return equal; half will not. Of the half that are unequal you know which item in the pair is 0 and which is 1, so those need no more comparisons.

提交回复
热议问题