Algorithm for N-way merge

How about the following idea:

  1. Create a priority queue
  2. Iterate through each file f
    1. enqueue the pair (nextNumberIn(f), f) using the first value as priority key
  3. While queue not empty
    1. dequeue head (m, f) of queue
    2. output m
    3. if f not depleted
      1. enqueue (nextNumberIn(f), f)

Since adding elements to a priority queue can be done in logarithmic time, item 2 is O(N × log N). Since (almost all) iterations of the while loop adds an element, the whole while-loop is O(M × log N) where M is the total number of numbers to sort.

Assuming all files have a non-empty sequence of numbers, we have M > N and thus the whole algorithm should be O(M × log N).

Leave a Comment