10

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

Community
  • 1
  • 1
JAN
  • 18,509
  • 49
  • 147
  • 268
  • 2
    Is there no space constraint? – biziclop Aug 20 '12 at 17:25
  • I've heard this called a few things, including a bucket sort and a math sort. You basically just allocate an array that's as big as your range, initialize the whole thing to zero, and iterate over your unsorted collection, and increment values in the array at the index of numbers in your collection. You can extend this to structures that are slightly more complex as well, but it's important to note that it doesn't scale well - it only works for numeric types and may require allocating gigantic arrays. For example, "sorting" 10000 ints in the range [0,10^8-1] will require about 200MB of RAM. – Wug Aug 20 '12 at 17:25
  • @biziclop: Nothing regarding space , just `O(n)` run-time. – JAN Aug 20 '12 at 17:26
  • @Wug That's the solution I was thinking about but wouldn't that also mean that you have to then scour the array to collect the results, which is O(n^2)? – biziclop Aug 20 '12 at 17:27
  • Yeah, its an issue. If you're dealing with large quantities of numbers its better because there will be less empty space. There is no general algorithm to sort things in less than N*log(N) time, so unless you're dealing with a very specific case that can be handled more efficiently, you're up a creek. – Wug Aug 20 '12 at 17:29
  • How big do you expect n to get? Seems like n^2 will be going beyond the normal int range for most programming languages pretty quickly, unless n is small. – mbeckish Aug 20 '12 at 17:37
  • 3
    Another question: how do you know it's possible? – biziclop Aug 20 '12 at 17:40
  • I know because I solved it now . O(n) as mentioned in the above link . – JAN Aug 21 '12 at 11:39
  • Change the base to n, then you have 2 digits for each element, based on Radix sort then two times cal Counting sort for each digit. – Emad Aghayi Aug 09 '19 at 02:32

2 Answers2

3

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)).

mbeckish
  • 10,108
  • 5
  • 27
  • 47
  • 4
    There exists a O(n) algorithm, see http://stackoverflow.com/questions/4238460/an-array-of-length-n-can-contain-values-1-2-3-n2-is-it-possible-to-sort-in. – sdcvvc Aug 20 '12 at 17:57
  • @sdcwc - If you have an O(n) solution, then post it as an answer, rather than just claiming its existence in a comment. Regarding your particular link, I believe that is only O(n) if the max number of digits is limited to a constant (such as 32-bit or 64-bit integers). Is that correct? – mbeckish Aug 20 '12 at 18:07
  • 3
    In RAM model, operations on word-size integers are O(1). For example, binary search is O(log n) [not O(log^2 n) if you counted bit operations]. I voted to close as a duplicate. – sdcvvc Aug 20 '12 at 18:13
  • 2
    Shouldn't `writing down k as a base N number` be an `O(logN)` operation? – biziclop Aug 20 '12 at 18:13
  • @biziclop: That's bit complexity. In the RAM model, it's O(1). For example, each step in binary search computes `(L+R)/2` but in analysis the addition is assumed to be done in `O(1)` time. – sdcvvc Aug 20 '12 at 18:22
3

The actual time will depend on the distribution of data that you have, but I would do the following:

  • Make n buckets.
  • Go through each number and put element with value i into bucket sqrt(i).
  • Go through each bucket, and perform radix sort on each element in the bucket.
Kirby
  • 3,348
  • 3
  • 19
  • 27
  • The range for each bucket would be order N (sqrt(N^2-1)). Radix sort takes time O(R N) where R is the number of bits which would be lg N in this case. In the worst case all the elements end up in the same bucket, so the last step still takes O(N lg N) – MWB Aug 20 '12 at 17:49
  • Agreed, worst case still sucks. Still, a lot of popular algorithms (eg. quicksort) are still bad in the worst case, which is why it depends a lot on your data distribution. If the data in the range is uniformly distributed, each bucket only has one element in it, and the sort completes in O(n). – Kirby Aug 20 '12 at 17:53