Thursday, August 10, 2006

N Sorted Arrays

Given n streams of sorted data (each of which can be infinite in length). Give an efficient way to merge them to give a single sorted stream..

Best method would be to store all the streams in a heap. With each node in the heap having the head value of the stream. So when we pop out one element from the heap. We push back the stream with the head removed and pushing the next element as the head. This way we can merge the streams quickly.

No comments: