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

前端 未结 8 1622
慢半拍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:29

    Using Radix Sort (In Ruby):

    def sort(array)
    sorted_array = Array.new(100,[])
    array.each do |t|
    sorted_array[t-1] = sorted_array[t-1] + [t]
    end
    sorted_array.flatten!
    end
    

提交回复
热议问题