What is the fastest way to sort 1 million integers when integers are from the range [1,100]?

前端 未结 8 1629
慢半拍i
慢半拍i 2020-12-23 22:13

Notes: I\'ve thought about Radix sort, bucket sort, counting sort.

Is there anyway to achieve big O(n)?

8条回答
  •  眼角桃花
    2020-12-23 22:26

    I assume, you mean you want to achieve a small O(n); then bucket sort would be fastest. In fact, since you know the range of the integers, then using bucket sort simply becomes a problem of counting the occurrences of the numbers which can be done in O(n), i.e. linear time.

    The so-called counting sort is simply a special case of bucket sort.

提交回复
热议问题