Search in sources :

Example 1 with Stack

use of edu.princeton.cs.algs4.Stack in project algorithms-sedgewick-wayne by reneargento.

the class Exercise43_ResizingArrayXLinkedList method timeTrial.

private double timeTrial(int n, Stack<Integer> stack) {
    int max = 1000000;
    int[] numbers = new int[n];
    for (int i = 0; i < n; i++) {
        numbers[i] = StdRandom.uniform(-max, max);
    }
    Stopwatch timer = new Stopwatch();
    for (int number : numbers) {
        stack.push(number);
    }
    while (!stack.isEmpty()) {
        stack.pop();
    }
    return timer.elapsedTime();
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 2 with Stack

use of edu.princeton.cs.algs4.Stack 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 3 with Stack

use of edu.princeton.cs.algs4.Stack in project algorithms-sedgewick-wayne by reneargento.

the class Exercise28_SelectionFilter method doExperimentToEstimateRunningTime.

private static void doExperimentToEstimateRunningTime(int arraySize, int numberOfExperiments) {
    // 10^4
    int m = 10000;
    for (int i = 0; i < numberOfExperiments; i++) {
        PriorityQueue<Point> priorityQueue = new PriorityQueue<>(m + 1, PriorityQueue.Orientation.MAX);
        Point[] pointArray = generateRandomPointsArray(arraySize);
        Stopwatch timer = new Stopwatch();
        for (Point point : pointArray) {
            priorityQueue.insert(point);
            if (priorityQueue.size() > m) {
                priorityQueue.deleteTop();
            }
        }
        Stack<Point> pointsStack = reversePointsOrder(priorityQueue);
        printPoints(pointsStack);
        double runningTime = timer.elapsedTime();
        StdOut.println("Running time for N = " + arraySize + " and M = " + m + ": " + runningTime);
        arraySize *= 10;
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 4 with Stack

use of edu.princeton.cs.algs4.Stack in project algorithms-sedgewick-wayne by reneargento.

the class Exercise42_CopyStack method main.

public static void main(String[] args) {
    Stack<Integer> originalStack = new Stack<>();
    originalStack.push(1);
    originalStack.push(2);
    originalStack.push(3);
    originalStack.push(4);
    Exercise42_CopyStack<Integer> stackCopy = new Exercise42_CopyStack<>(originalStack);
    stackCopy.push(5);
    stackCopy.push(6);
    originalStack.pop();
    originalStack.pop();
    stackCopy.pop();
    StringJoiner originalStackItems = new StringJoiner(" ");
    for (int item : originalStack) {
        originalStackItems.add(String.valueOf(item));
    }
    StdOut.println("Original Stack: " + originalStackItems.toString());
    StdOut.println("Expected: 2 1");
    StdOut.println();
    StringJoiner copyStackItems = new StringJoiner(" ");
    for (int item : stackCopy) {
        copyStackItems.add(String.valueOf(item));
    }
    StdOut.println("Stack Copy: " + copyStackItems.toString());
    StdOut.println("Expected: 5 4 3 2 1");
}
Also used : StringJoiner(java.util.StringJoiner) Stack(edu.princeton.cs.algs4.Stack)

Example 5 with Stack

use of edu.princeton.cs.algs4.Stack in project algorithms-sedgewick-wayne by reneargento.

the class Exercise47_CatenableCollections method testStackCatenation.

private static void testStackCatenation(Exercise47_CatenableCollections<Integer> catenableCollections) {
    // Stack catenation test
    Stack<Integer> stack1 = new Stack<>();
    stack1.push(0);
    stack1.push(1);
    stack1.push(2);
    stack1.push(3);
    Stack<Integer> stack2 = new Stack<>();
    stack2.push(7);
    stack2.push(8);
    stack2.push(9);
    catenableCollections.catenationStacks(stack1, stack2);
    StringJoiner stack1Items = new StringJoiner(" ");
    for (int item : stack1) {
        stack1Items.add(String.valueOf(item));
    }
    StdOut.println("Stack 1 after catenation: " + stack1.toString());
    StdOut.println("Expected: 9 8 7 3 2 1 0");
    StdOut.println();
    StringJoiner stack2Items = new StringJoiner(" ");
    for (int item : stack2) {
        stack2Items.add(String.valueOf(item));
    }
    StdOut.println("Stack 2 after catenation: " + stack2Items.toString());
    StdOut.println("Expected: ");
    StdOut.println();
}
Also used : StringJoiner(java.util.StringJoiner) Stack(edu.princeton.cs.algs4.Stack)

Aggregations

Stack (edu.princeton.cs.algs4.Stack)2 Stopwatch (edu.princeton.cs.algs4.Stopwatch)2 StringJoiner (java.util.StringJoiner)2 Stack (com.jimmysun.algorithms.chapter1_3.Stack)1 Transaction (com.jimmysun.algorithms.chapter2_1.Transaction)1 MinPQ (edu.princeton.cs.algs4.MinPQ)1