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?

  1. A. 3
  2. B. 6
  3. C. 4
  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

Exams that ask this topic