Question: In given K input streams of number in sorted order. You need to make a single output stream, which contains all the elements of the

In given K input streams of number in sorted order. You need to make a single output stream, which contains all the elements of the K streams in sorted order. The input streams support ReadNumber() operation and output stream support WriteNumber() operation.

a) Read the first number from all the K input streams and add them to a Priority Queue. (Nodes should keep track of the input stream;

data added to the PQ is value & stream id.)

b) Dequeue one element at a time from PQ, Put this element value to the output stream, Read the input stream number and from the same input stream add another element to PQ.

c) If the stream is empty, just continue

d) Repeat until PQ is empty.

Step by Step Solution

3.18 Rating (151 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

here is the algorithm to merge K sorted streams into a single sorted stream Create a priority queue ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Data Structures Algorithms Questions!