GATE Computer ScienceEasyGATE CS (PYQ pattern)

Counting sort (stable, processing the input from right to left with cumulative counts) is applied to a = [4, 2, 2, 8, 3, 3, 1, 4, 2]. At which 0-based index of the output array is the element that sits at index 2 of the input (the second 2 from the left) placed?

  1. A. 1
  2. B. 3
  3. C. 4
  4. D. 2

Correct answer: D. 2

The sorted output is 1, 2, 2, 2, 3, 3, 4, 4, 8; the three 2s occupy output indices 1, 2, 3 in input order (indices 1, 2, 8), so the 2 from input index 2 goes to output index 2. Stability is what pins this down.

More Algorithms questions

Exams that ask this topic