GATE Computer ScienceVery hardGATE CS (PYQ pattern)
Top-down merge sort splits n elements into a left part of ⌈n/2⌉ and a right part of ⌊n/2⌋, and its merge stops comparing as soon as one part is exhausted (then copies the rest). How many key comparisons does it make on an input of 100 distinct keys that is already sorted in increasing order?
- A. 316
- B. 356
- C. 573
- D. 332
Correct answer: B. 356
On sorted input every left element is smaller than every right element, so a merge makes exactly |left| = ⌈m/2⌉ comparisons (not min(n₁, n₂)). Summing ⌈m/2⌉ over the recursion: C(100) = 2C(50) + 50, C(50) = 2C(25) + 25, C(25) = C(13) + C(12) + 13, … gives 356. Using ⌊m/2⌋ gives 316 (that is what reverse-sorted input costs); 573 is the worst case.
More Algorithms questions
- The keys 3, 9, 2, 1, 4, 5, 7, 8, 6, 10, 11, 12 are inserted, in this order, one at a time into an initially empty binary max-heap, each…
- The array [5, 3, 17, 10, 84, 19, 6, 22, 9, 15, 2] is turned into a max-heap by Floyd's linear-time Build-Max-Heap. Which array results?…
- Insertion sort on [7, 3, 9, 1, 8, 2, 6, 5, 4] scans left-to-right from position j = i − 1, comparing a[j] with the key until a[j] ≤ key or…
- Lomuto partition (pivot = last element, loop j from lo to hi−1 comparing a[j] ≤ pivot) is used in quicksort on [5, 2, 8, 1, 9, 3, 7, 4…
- A randomised quicksort on a random permutation of n distinct keys picks a pivot uniformly and every partition of m elements costs exactly…
- Binary search on a sorted array of 20 distinct keys uses mid = ⌊(lo + hi)/2⌋ and one three-way comparison per probe, stopping when lo >…