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
here is the algorithm to merge K sorted streams into a single sorted stream Create a priority queue ... View full answer
Get step-by-step solutions from verified subject matter experts
