/****************************************************************************** * Compilation: javac Multiway.java * Execution: java Multiway input1.txt input2.txt input3.txt ... * Dependencies: IndexMinPQ.java In.java StdOut.java * Data files: https://algs4.cs.princeton.edu/24pq/m1.txt * https://algs4.cs.princeton.edu/24pq/m2.txt * https://algs4.cs.princeton.edu/24pq/m3.txt * * Merges together the sorted input stream given as command-line arguments * into a single sorted output stream on standard output. * * % more m1.txt * A B C F G I I Z * * % more m2.txt * B D H P Q Q * * % more m3.txt * A B E F J N * * % java Multiway m1.txt m2.txt m3.txt * A A B B B C D E F F G H I I J N P Q Q Z * ******************************************************************************/ /** * The {@code Multiway} class provides a client for reading in several * sorted text files and merging them together into a single sorted * text stream. * This implementation uses a {@link IndexMinPQ} to perform the multiway * merge. *
* For additional documentation, see Section 2.4
* of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Multiway {
// This class should not be instantiated.
private Multiway() { }
// merge together the sorted input streams and write the sorted result to standard output
private static void merge(In[] streams) {
int n = streams.length;
IndexMinPQ