Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a sequence in O(n) time [duplicate]

Tags:

java

c

c#

algorithm

Possible Duplicate:
Sorting in linear time?

Suppose we are given a sequence S of n elements, each of which is an integer in the range [0,n^2-1]. Can we sort it in O(n) time?

Please dont mind me asking too many interview questions. I am just appetent.

like image 367
WebDev Avatar asked Mar 21 '11 14:03

WebDev


1 Answers

No.

When the only precondition is an integer in the range 0-N².

  • Counting sorts won't work because the scanning, be it bit-patterns for distinct inputs or buckets for duplicate inputs, would complete in O(N²)
  • The range would make the key length for radix sort dependent on N hence radix won't work in O(N).

Any statement involving "When N is small" invalidates any O based argument.

like image 131
Captain Giraffe Avatar answered Oct 24 '22 04:10

Captain Giraffe