Data Structures19 sections · 729 units
Open in Course

The Merge Problem

K-way merge

Given kk sorted lists, merge them into one sorted list. Approaches:

1.1. Merge two at a time: merge first two, then merge result with third, etc. Time: O(nk)O(nk) where nn is total elements.

2.2. Min-heap: always pick the smallest among current heads. Time: O(nlogk)O(n \log k). The heap approach is much faster when kk is large. The heap tracks the "frontier" of kk candidates.