GATE Computer ScienceHardGATE CS (PYQ pattern)
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 is the number of element shifts (assignments a[j] = a[j − 5]). How many shifts occur?
- A. 3
- B. 6
- C. 4
- D. 8
Correct answer: C. 4
The pass gives [8, 12, 19, 3, 2, 23, 28, 45, 56, 67, 41, 34]. Exactly four elements (8, 19, 2 and 28) each jump over one larger element five places back, so 4 shifts occur.
More Algorithms questions
- 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…
- Vertices 0–10 of the tree with edges 0-1, 1-2, 1-3, 3-4, 3-5, 5-6, 0-7, 7-8, 2-9, 9-10 carry weights w = 4, 7, 3, 9, 5, 2, 8, 6, 5, 4, 10…
- Five cities have symmetric distances d(0,1) = 9, d(0,2) = 14, d(0,3) = 23, d(0,4) = 17, d(1,2) = 11, d(1,3) = 19, d(1,4) = 26, d(2,3) = 8…