Sort n numbers between [0,n^2 - 1] in O(n)?

JAN picture JAN · Aug 20, 2012 · Viewed 8.5k times · Source

Possible Duplicate:
An array of length N can contain values 1,2,3 … N^2. Is it possible to sort in O(n) time?

Given n numbers at the range [0,n^2 -1] how can we sort them in O(n) run time ?

I have a feeling that the solution involves radix sort ,but I'm still missing something.

The n numbers are integers .

Any ideas ?

REMARK: not homework!

Regards

Answer

mbeckish picture mbeckish · Aug 20, 2012

I think you're out of luck. Radix sort is O(k*n), where k is number of digits. In your case, k = log(n^2), resulting in O(n*log(n)).