Ejemplo n.º 1
0
    private static void merge(int[] streams)
    {
        int n = streams.Length;

        IndexMinPQ <string> pq = new IndexMinPQ <string>(n);

        for (int i = 0; i < n; i++)
        {
            if (!streams[i].Equals(null))
            {
                pq.insert(i, streams[i].ToString());
            }
        }

        // Extract and print min and read next from its stream.
        while (!pq.isEmpty())
        {
            print(pq.minKey() + " ");
            int i = pq.delMin();
            if (!streams[i].Equals(null))
            {
                pq.insert(i, streams[i].ToString());
            }
        }
    }
Ejemplo n.º 2
0
    // 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);
        for (int i = 0; i < n; i++)
            if (!streams[i].isEmpty())
                pq.insert(i, streams[i].readString());

        // Extract and print min and read next from its stream. 
        while (!pq.isEmpty()) {
            StdOut.print(pq.minKey() + " ");
            int i = pq.delMin();
            if (!streams[i].isEmpty())
                pq.insert(i, streams[i].readString());
        }
        StdOut.println();
    }