Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. public class Multiway {
  2.  
  3.     // This class should not be instantiated.
  4.     private Multiway() { }
  5.    
  6.     // merge together the sorted input streams and write the sorted result to standard output
  7.     private static void merge(In[] streams) {
  8.         int N = streams.length;
  9.        
  10.         IndexMinPQ<String> pq = new IndexMinPQ<String>(N);
  11.         Out out = new Out("out.txt");
  12.         Out m = new Out("m.txt");
  13.        
  14.         for (int i = 0; i < N; i++) {
  15.             if (!streams[i].isEmpty()) {
  16.                 String key = streams[i].readString();
  17.                 pq.insert(i, key);
  18.                 out.println("insert " + i + ", " +key);
  19.                 }
  20.         }
  21.        
  22.         Queue<String> minkeys = new Queue<>();
  23.        
  24.         // Extract and print min and read next from its stream.
  25.         while (!pq.isEmpty()) {
  26.            
  27.             out.println("MINPQ");
  28.             for (Integer k : pq) {
  29.                 out.println(k + ":" +pq.keyOf(k));
  30.             }
  31.            
  32.             //StdOut.print(pq.minKey() + " ");
  33.            
  34.             out.println("minkey " + pq.minIndex() + " : " +pq.minKey());
  35.             minkeys.enqueue(pq.minKey());
  36.            
  37.             for(String el : minkeys)
  38.                 m.print(el + " ");
  39.            
  40.            
  41.             int i = pq.delMin();
  42.             if (!streams[i].isEmpty()) {
  43.                 String key = streams[i].readString();
  44.                 pq.insert(i, key);
  45.                 out.println("insert " + i + ", " +key);
  46.             }
  47.            
  48.         }
  49.        
  50.         StdOut.println();
  51.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement