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?
- A. 1
- B. 3
- C. 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
- LSD radix sort (decimal digits, stable counting sort on each digit, least significant first) is applied to [329, 457, 657, 839, 436, 720…
- A shell sort pass with gap 5 (a gapped insertion sort) is applied to [23, 12, 45, 3, 67, 8, 34, 19, 56, 2, 41, 28]. The cost of the pass…
- The array [14, 5, 21, 9, 30, 7, 18, 2, 26, 11] is made into a max-heap, giving [30, 26, 21, 14, 11, 7, 18, 2, 9, 5]. Heapsort then…
- In how many ways can 30 be written as a sum of ODD positive integers, where order does not matter and parts may repeat?
- In how many ways can 20 be written as a sum of exactly 4 positive integers, where order does not matter (parts may repeat)?
- A tree on vertices 0–10 has weighted edges 0-1 (3), 1-2 (4), 1-3 (2), 3-4 (6), 3-5 (1), 5-6 (7), 0-7 (5), 7-8 (2), 2-9 (3), 9-10 (8). Its…