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

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

    You can use counting sort.

    Counting sort (sometimes referred to as ultra sort or math sort) is a sorting algorithm which (like bucket sort) takes advantage of knowing the range of the numbers in the array to be sorted (array A).

    Counting sort is a stable sort and has a running time of Θ(n+k), where n and k are the lengths of the arrays A (the input array) and C (the counting array), respectively. In order for this algorithm to be efficient, k must not be much larger than n.

    In this case k is 100 and n is 1000000.

提交回复
热议问题