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?
- A. 21
- B. 23
- C. 25
- 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
- A sorted array holds 1000 distinct elements and binary search (one three-way comparison per probe) is applied. What is the worst-case…
- What is the asymptotic solution of T(n) = 3T(n/2) + n² with T(1) = 1?
- Selection sort (find the minimum of the unsorted part and exchange it with the first unsorted position, skipping the exchange when the…
- How many inversions (pairs i < j with A[i] > A[j]) does the array [8, 4, 2, 1, 7, 3, 6, 5] have?
- A max-heap is built from [3, 9, 2, 1, 4, 5, 8, 7] by the bottom-up method (Max-Heapify from the last internal node down to the root…
- A file uses six symbols with frequencies 5, 9, 12, 13, 16 and 45. What is the total number of bits needed to encode the file with an…