GATE / CS ARCHIVE

GATE CSE 2005 / QUESTION 39

Merging logarithmically many sorted lists

Algorithms · Searching, Sorting & Selection · Full GATE CSE 2005 paper

GATE 2005, Q39 2 marks · MCQ
Report issue

Searching, Sorting & Selection

Question 39, GATE 2005: Merge roughly log n sorted lists, each containing roughly n/log n elements, using a heap. Options include A O(n log log n), B Θ(n log n), C Ω(n log n), D Ω(n^(3/2)). Find the intended efficient merge bound.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Keep the smallest remaining item from each of k=Θ(log n) lists in a min-heap. Repeatedly extract the minimum and replace it by the next item from the same list. There are Θ(n) items, each costing O(log k)=O(log log n), so the merge costs O(n log log n), A. The comparison lower bound for arbitrary interleavings of equally sized lists is Ω(n log k), so this is also the worst-case asymptotic order.

The heap needs one current head per list, only Θ(log n) elements. Removing the smallest head and inserting its successor costs O(log log n), not O(log n), because heap height depends on list count. Repeating for Θ(n) output elements gives O(n log log n). The lists are already internally sorted, so sorting their whole union from scratch would waste that available order.

Original paper, page 5