GATE Computer ScienceHardGATE CS

Binary insertion sort sorts 10 distinct elements. It inserts the k-th element into the sorted prefix of k − 1 elements by binary search over the prefix. What is the worst-case total number of key comparisons spent on locating insertion positions?

  1. A. 21
  2. B. 23
  3. C. 25
  4. D. 27

Correct answer: C. 25

Locating a position among k sorted elements takes at most ⌈log₂(k + 1)⌉ comparisons. For k = 1 to 9 this is 1, 2, 2, 3, 3, 3, 3, 4, 4, whose sum is 25 (shifting elements costs moves, not comparisons).

More Algorithms questions

Exams that ask this topic