An efficient way of sequencing streams of integers



I have N streams of integers. Each stream returns integers, one at a time, in ascending order. I need to merge the streams so that that the next integer is always the lowest available integer across the streams. The streams are of random length. The length of each stream is known in advance.

For example,

stream 0: 1,2,3,4,5
stream 1: 2,4,6
stream 2: 3,4,7,8

The output should look like a simple sort: 1,2,2,3,3,4,4,4,5,6,7,8

The naive way to do it is for each integer in the output, look through all of the streams, find the lowest one, and return it. The cost of this algorithm is O(N * M), where N is the number of streams and M is the total number of integers. (This formula doesn't take into account that some streams may run out before the end, reducing the number that must be considered on each operation).

Is there a faster algorithm out there? This one doesn't scale well when there is a very large number of streams.
.