8

Given an array of length N. It can contain values from ranging from 1 to N^2 (N squared) both inclusive, values are integral. Is it possible to sort this array in O(N) time? If possible how?

Edit: This is not a homework.

CodesInChaos
  • 100,017
  • 20
  • 197
  • 251
riderchap
  • 639
  • 1
  • 10
  • 19

3 Answers3

10

Write each integer in base N, that is each x can be represented as (x1, x2) with x = 1 + x1 + x2*N. Now you can sort it twice with counting sort, once on x1 and once on x2, resulting in the sorted array.

EDIT: As others mentioned below, sorting on each 'digit' separately like this is is called a radix sort. Sorting on each digit with counting sort takes O(N) time and O(N) space (in this particular case). Since we repeat it exactly twice, this gives a total running time of O(N).

Yakov Galka
  • 61,035
  • 13
  • 128
  • 192
  • A two phase bucket sort. You first count how many entries a bucket will have, and from that the start index of each bucket(takes O(n)). Then you can inplace swap the entries into each bucket in O(n) too. – CodesInChaos Nov 21 '10 at 15:07
  • I think the rest of the world calls this a radix sort. – Omnifarious Nov 21 '10 at 15:13
  • 2
    -1 because I think this answer is significantly less clear than @svick's answer. – Omnifarious Nov 21 '10 at 15:18
  • I've only heard the term "counting sort" used to describe a procedure in which duplicated keys will cause duplicated records to be output. The term "bucket sort" would normally be used to describe a single-pass stable partitioning operation, and "radix sort" to describe multi-pass partitioning. – supercat May 07 '12 at 22:13
  • 1
    @supercat: See [Counting sort](http://en.wikipedia.org/wiki/Counting_sort). Bucket sort is an alternative algorithm that has the same complexity as Counting sort. Radix sort is a sorting algorithm that sorts arbitrary lists of integers by applying [counting sort or bucket sort](http://en.wikipedia.org/wiki/Radix_sort#Definition) repeatedly to sort each digit separately. Note that it's crucial for Radix sort to work that both Counting sort and Bucket sort are stable. So basically I don't understand how your comment is relevant. – Yakov Galka May 07 '12 at 22:22
  • Counting sort on base 10 needs you to go from 0 -> 9, and find all elements in that order, so it will be O(10 * N) = O(n). If you are using base n, you'll need to look for all values in each digit from 0 -> n - 1, so counting sort will need O(n * n) = O(n^2), no? – maged Dec 19 '13 at 17:34
  • @ybungalobill i am not able to understand `x = 1 + x1 + x2*N. ` . If we represent number in base `N` , N^2 can be expressed in maximum 2 bits , x1 and x2 then `x = x1 * n^0 + x2 * n^1 ` = `x1 + x2 * N` . please clarify . – Aseem Goyal Jan 24 '14 at 16:29
  • @anon: `N^2` is *three* digits in base `N`, namely `100`. The numbers `0, ..., N^2-1` inclusive are all the two-digit numbers. Hence you need to add one to get `1, ..., N^2` as in the OP. – Yakov Galka Jan 27 '14 at 18:22
8

Yes, you can, using radix sort with N buckets and two passes. Basically, you treat the numbers as having 2 digits in base N.

svick
  • 214,528
  • 47
  • 357
  • 477
  • i am not able to understand `x = 1 + x1 + x2*N` as told in accepted answer . If we represent number in base `N` , `N^2` can be expressed in maximum 2 bits , x1 and x2 then `x = x1 * n^0 + x2 * n^1` = `x1 + x2 * N` . please clarify . – Aseem Goyal Jan 25 '14 at 07:49
0

It is possible to sort any array of integers with a well defined maximum value in O(n) time using a radix sort. This is likely the case for any list of integers you encounter. For example if you were sorting a list of arbitrary precision integers it wouldn't be true. But all the C integral types have well-defined fixed ranges.

Omnifarious
  • 50,447
  • 15
  • 117
  • 181