UGC-NET Dec 2013 · Heap Sort & Non-Comparison Sorts

MCQ · 2 marks · Problem PROG-SORT-0006

Assuming there are n keys and each key is in the range [0, m-1]. The run time of bucket sort is
  1. A
    O(n)
  2. B
    O(n lgn)
  3. C
    O(n lgm)
  4. D
    O(n+m)

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solutionWith integer keys in \([0,m-1]\), bucket (counting-style) sort uses one bucket per key value: create \(m\) empty buckets: \(O(m)\); put each of the \(n\) keys into its bucket: \(O(n)\); concatenate the buckets in order: \(O(n+m)\).\[T=O(n+m).\]It is linear when \(m=O(n)\), but in general the \(m\) term cannot be dropped.Answer: D

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com