Data Structures & AlgorithmsModerateTCS Digital · Infosys SP

You need to sort 10⁷ integers, each in the range 0 to 999. Which approach is asymptotically fastest?

  1. A. Quick sort, average O(n log n)
  2. B. Heap sort, O(n log n)
  3. C. Counting sort, O(n + k)
  4. D. Merge sort, O(n log n)

Correct answer: C. Counting sort, O(n + k)

With only k = 1000 possible values, counting sort runs in O(n + k) = O(n), beating every comparison sort's Ω(n log n) lower bound.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic