标签:index nbsp rgs 插入 min() IV port client int
import edu.princeton.cs.algs4.*; public class Multiway { public static void merge(In[] streams) { int N = streams.length; IndexMinPQ<String> pq = new IndexMinPQ<String>(N); for(int i = 0; i < N; i++) { if(!streams[i].isEmpty()) pq.insert(i,streams[i].readString()); } while(!pq.isEmpty())//也就是这里的pq大多数时间一直保持三个元素,最后会因为没有输入而减少元素个数 { StdOut.println(pq.minKey());//IndexMinPQ没有min方法,只有minKey(),应该是书本错误 int i = pq.delMin(); if(!streams[i].isEmpty())//若删除的那一行streams非空,就再插入一个该行的元素 pq.insert(i,streams[i].readString()); } } public static void main(String[] args) { //输入的本来就是有序的了 int N = args.length; In[] streams = new In[N]; for(int i = 0; i < N; i++) streams[i] = new In(args[i]); merge(streams); } }
322 Multiway merge priority queue client
标签:index nbsp rgs 插入 min() IV port client int
原文地址:https://www.cnblogs.com/w-j-c/p/9132472.html