C
Chris
Does anyone know of an efficient algorithm for merging many streams of
objects?
Suppose I have N streams of words. Each stream returns words in
alphabetical sequence. For example:
stream 0: aardvark, bear, dog
stream 1: cat, groundhog
stream 3: eagle, fox
I want the output to be:
aardvark, bear, cat, dog, eagle, fox, groundhog.
The number of words can be large (in the millions) and the number of
streams can also be large (in the thousands).
The brute force approach is to create an array of streams, get the
lowest one from each, and output the lowest word found. Repeat until all
streams exhausted. The problem is that the running time is O(N * M),
where N is the number of streams and M is the *total number of elements
across all streams*. This is obviously not scalable.
objects?
Suppose I have N streams of words. Each stream returns words in
alphabetical sequence. For example:
stream 0: aardvark, bear, dog
stream 1: cat, groundhog
stream 3: eagle, fox
I want the output to be:
aardvark, bear, cat, dog, eagle, fox, groundhog.
The number of words can be large (in the millions) and the number of
streams can also be large (in the thousands).
The brute force approach is to create an array of streams, get the
lowest one from each, and output the lowest word found. Repeat until all
streams exhausted. The problem is that the running time is O(N * M),
where N is the number of streams and M is the *total number of elements
across all streams*. This is obviously not scalable.