Why we can not apply counting sort to general arrays?

自闭症网瘾萝莉.ら 提交于 2019-11-29 08:01:59

It is not enough to know the upper bound to run a counting sort: you need to have enough memory to fit all the counters.

Consider a situation when you go through an array of 64-bit integers, and find out that the largest element is 2^60. This would mean two things:

  • You need an O(2^60) memory, and
  • It is going to take O(2^60) to complete the sort.

The fact that O(2^60) is the same as O(1) is of little help here, because the constant factor is simply too large. This is very often a problem with pseudo-polynomial time algorithms.

Suppose the largest number is like 235684121. Then you'll spend incredible amounts of RAM to keep your buckets.

I would like to mention something with @dasblinkenlight and @AlbinSunnanbo answers, your idea to scan the array in O(n) pass, to find the maximum value in the array is okay. Below is given from Wikipedia:

However, if the value of k is not already known then it may be computed by an additional loop over the data to determine the maximum key value that actually occurs within the data.

As the time complexity is O(n + k) and k should be under a certain limit, your found k should be small. As @dasblinkenlight mentioned, O(large_value) can't practically be converged to O(1).

Though I don't know about any major applications of Counting sort so far except used as a subroutine of Radix Sort, it can be nicely used in problems like string sorting( i.e. sort "android" to "addnoir") as here k is only 255.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!