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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…