Can you sort n integers in O(n) amortized complexity?

前端 未结 4 2201
星月不相逢
星月不相逢 2021-01-05 01:38

Is it theoretically possible to sort an array of n integers in an amortized complexity of O(n)?

What about trying to create a worst case of O(n) complexity?

4条回答
  •  情书的邮戳
    2021-01-05 02:10

    I'm not quite happy with the accepted answer so far. So I'm retrying an answer:

    Is it theoretically possible to sort an array of n integers in an amortized complexity of O(n)?

    The answer to this question depends on the machine that would execute the sorting algorithm. If you have a random access machine, which can operate on exactly 1 bit, you can do radix sort for integers with at most k bits, which was already suggested. So you end up with complexity O(kn).
    But if you are operating on a fixed size word machine with a word size of at least k bits (which all consumer computers are), the best you can achieve is O(n log n). This is because either log n < k or you could do a count sort first and then sort with a O (n log n) algorithm, which would yield the first case also.

    What about trying to create a worst case of O(n) complexity?

    That is not possible. A link was already given. The idea of the proof is that in order to be able to sort, you have to decide for every element to be sorted if it is larger or smaller to any other element to be sorted. By using transitivity this can be represented as a decision tree, which has n nodes and log n depth at best. So if you want to have performance better than Ω(n log n) this means removing edges from that decision tree. But if the decision tree is not complete, than how can you make sure that you have made a correct decision about some elements a and b?

    Can you with no limitation on memory usage create such an algorithm?

    So as from above that is not possible. And the remaining questions are therefore of no relevance.

提交回复
热议问题