Is there an O(n) integer sorting algorithm?
Yes, Radix Sort and Counting Sort are O(N)
. They are NOT comparison-based sorts, which have been proven to have Ω(N log N)
lower bound.
To be precise, Radix Sort is O(kN)
, where k
is the number of digits in the values to be sorted. Counting Sort is O(N + k)
, where k
is the range of the numbers to be sorted.
There are specific applications where k
is small enough that both Radix Sort and Counting Sort exhibit linear-time performance in practice.
Comparison sorts must be at least Ω(n log n) on average.
However, counting sort and radix sort scale linearly with input size – because they are not comparison sorts, they exploit the fixed structure of the inputs.
Counting sort: http://en.wikipedia.org/wiki/Counting_sort if your integers are fairly small. Radix sort if you have bigger numbers (this is basically a generalization of counting sort, or an optimization for bigger numbers if you will): http://en.wikipedia.org/wiki/Radix_sort
There is also bucket sort: http://en.wikipedia.org/wiki/Bucket_sort