Search in sources :

Example 1 with MinPQ

use of edu.princeton.cs.algs4.MinPQ in project AlgorithmsSolutions by Allenskoo856.

the class TopM method main.

public static void main(String[] args) {
    int M = Integer.parseInt(args[0]);
    MinPQ<Transaction> pq = new MinPQ<Transaction>(M + 1);
    while (StdIn.hasNextLine()) {
        pq.insert(new Transaction(StdIn.readLine()));
        if (pq.size() > M) {
            pq.delMin();
        }
    }
    Stack<Transaction> stack = new Stack<Transaction>();
    while (!pq.isEmpty()) {
        stack.push(pq.delMin());
    }
    for (Transaction t : stack) {
        StdOut.println(t);
    }
}
Also used : Transaction(com.jimmysun.algorithms.chapter2_1.Transaction) MinPQ(edu.princeton.cs.algs4.MinPQ) Stack(com.jimmysun.algorithms.chapter1_3.Stack)

Example 2 with MinPQ

use of edu.princeton.cs.algs4.MinPQ in project AlgorithmsSolutions by Allenskoo856.

the class CollisionSystem method simulate.

public void simulate(double limit, double Hz) {
    pq = new MinPQ<Event>();
    for (int i = 0; i < particles.length; i++) {
        predictCollisions(particles[i], limit);
    }
    pq.insert(new Event(0, null, null));
    while (!pq.isEmpty()) {
        Event event = pq.delMin();
        if (!event.isValid()) {
            continue;
        }
        for (int i = 0; i < particles.length; i++) {
            particles[i].move(event.time - t);
        }
        t = event.time;
        Particle a = event.a, b = event.b;
        if (a != null && b != null) {
            a.bounceOff(b);
        } else if (a != null && b == null) {
            a.bounceOffVerticalWall();
        } else if (a == null && b != null) {
            b.bounceOffHorizontalWall();
        } else if (a == null && b == null) {
            redraw(limit, Hz);
        }
        predictCollisions(a, limit);
        predictCollisions(b, limit);
    }
}
Also used : Particle(edu.princeton.cs.algs4.Particle)

Aggregations

Stack (com.jimmysun.algorithms.chapter1_3.Stack)1 Transaction (com.jimmysun.algorithms.chapter2_1.Transaction)1 MinPQ (edu.princeton.cs.algs4.MinPQ)1 Particle (edu.princeton.cs.algs4.Particle)1