Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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<String> pq = new IndexMinPQ<String>(N);
- Out out = new Out("out.txt");
- Out m = new Out("m.txt");
- for (int i = 0; i < N; i++) {
- if (!streams[i].isEmpty()) {
- String key = streams[i].readString();
- pq.insert(i, key);
- out.println("insert " + i + ", " +key);
- }
- }
- Queue<String> minkeys = new Queue<>();
- // Extract and print min and read next from its stream.
- while (!pq.isEmpty()) {
- out.println("MINPQ");
- for (Integer k : pq) {
- out.println(k + ":" +pq.keyOf(k));
- }
- //StdOut.print(pq.minKey() + " ");
- out.println("minkey " + pq.minIndex() + " : " +pq.minKey());
- minkeys.enqueue(pq.minKey());
- for(String el : minkeys)
- m.print(el + " ");
- int i = pq.delMin();
- if (!streams[i].isEmpty()) {
- String key = streams[i].readString();
- pq.insert(i, key);
- out.println("insert " + i + ", " +key);
- }
- }
- StdOut.println();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement