Basic premise on counting sort. How is k related to to Big Oh

algorithmscomputer sciencesortingtheory

I am reading (Cormen) on counting sort.
I understand the structure of the algorithm but the statement:

In practice, we usually use counting sort when we have k = O(n), in
which case the running time is Theta(n).

Is not clear to my mind.

k is just the range of integers expected in the input.

Why is the asymptotic notation used in this statement for k? I don't get it.

Best Answer

Your quote is saying that if the range of integers k is linearly proportional to the total number of elements in the collection n, then a counting sort will have a best-case and a worst-case time complexity of O(n), making it a very efficient sort in such a case.

Big Oh (and others in the Bachman-Landau family of notations) are just simplifications of the precise complexity of the function in any given case, used to illustrate the increasing value of the function as N grows large. In programming, Big Oh is usually used in the context of time complexity; for n values, a function f(n) will execute in a time on the order of g(n), and thus we say it has a Big Oh complexity of O(g(n)). However the mathematical construct of Big Oh isn't so limited. In this particular case, it is being used to refer to the general relationship between k and N as numbers.

Simply put, when k (the range of the values of an n-element collection) increases as n does on the order of O(n), then the counting sort will be efficient. This will be true if, say, the list contained all multiples of 4 between 1 and x (in which case k~=x, and n=x/4, meaning that k ~= 4n = O(n)). The condition k=O(n) would be false for, say, the set of all perfect squares from 1 to x2 (in which case k=x2 but n=x, so k = n2 = O(n2)). In that case, k increases on the square (O(n2)) when N increases linearly (O(n)), so the counting sort would execute in the more complex time (which would be O(n2)), which would perform worse than some other sort implementation that ran in Theta(nlogn).