Search in sources :

Example 6 with Queue

use of com.jimmysun.algorithms.chapter1_3.Queue in project algorithms-sedgewick-wayne by reneargento.

the class BucketSort method bucketSort.

public void bucketSort(String[] strings) {
    int alphabetSize = 256;
    Queue<String>[] buckets = new Queue[alphabetSize + 1];
    for (int bucket = 0; bucket < buckets.length; bucket++) {
        buckets[bucket] = new Queue();
    }
    // 1- Put strings in buckets
    for (int i = 0; i < strings.length; i++) {
        int leadingDigitIndex = strings[i].charAt(0);
        buckets[leadingDigitIndex].enqueue(strings[i]);
    }
    // 2- Sort the sublists
    for (int bucket = 0; bucket < buckets.length; bucket++) {
        if (!buckets[bucket].isEmpty()) {
            sortBucket(buckets[bucket]);
        }
    }
    // 3- Stitch together all the buckets
    int arrayIndex = 0;
    for (int r = 0; r <= alphabetSize; r++) {
        while (!buckets[r].isEmpty()) {
            String currentString = buckets[r].dequeue();
            strings[arrayIndex++] = currentString;
        }
    }
}
Also used : Queue(chapter1.section3.Queue)

Example 7 with Queue

use of com.jimmysun.algorithms.chapter1_3.Queue in project algorithms-sedgewick-wayne by reneargento.

the class Exercise22_TransactionSortTest method readAllTransactions.

public static Transaction[] readAllTransactions(String fileName) {
    In in = new In(fileName);
    Queue<Transaction> queue = new Queue<>();
    while (!in.isEmpty()) {
        queue.enqueue(new Transaction(in.readLine()));
    }
    int queueSize = queue.size();
    Transaction[] transactions = new Transaction[queueSize];
    for (int i = 0; i < queueSize; i++) {
        transactions[i] = queue.dequeue();
    }
    return transactions;
}
Also used : Transaction(edu.princeton.cs.algs4.Transaction) In(edu.princeton.cs.algs4.In) Queue(edu.princeton.cs.algs4.Queue)

Example 8 with Queue

use of com.jimmysun.algorithms.chapter1_3.Queue in project algorithms-sedgewick-wayne by reneargento.

the class Exercise47_CatenableCollections method testQueueCatenation.

private static void testQueueCatenation(Exercise47_CatenableCollections<Integer> catenableCollections) {
    // Queue catenation test
    Queue<Integer> queue1 = new Queue<>();
    queue1.enqueue(0);
    queue1.enqueue(1);
    queue1.enqueue(2);
    queue1.enqueue(3);
    Queue<Integer> queue2 = new Queue<>();
    queue2.enqueue(7);
    queue2.enqueue(8);
    queue2.enqueue(9);
    catenableCollections.catenationQueues(queue1, queue2);
    StringJoiner queue1Items = new StringJoiner(" ");
    for (int item : queue1) {
        queue1Items.add(String.valueOf(item));
    }
    StdOut.println("Queue 1 after catenation: " + queue1Items.toString());
    StdOut.println("Expected: 0 1 2 3 7 8 9");
    StdOut.println();
    StringJoiner queue2Items = new StringJoiner(" ");
    for (int item : queue2) {
        queue1Items.add(String.valueOf(item));
    }
    StdOut.println("Queue 2 after catenation: " + queue2Items.toString());
    StdOut.println("Expected: ");
    StdOut.println();
}
Also used : Queue(edu.princeton.cs.algs4.Queue) StringJoiner(java.util.StringJoiner)

Example 9 with Queue

use of com.jimmysun.algorithms.chapter1_3.Queue in project algorithms-sedgewick-wayne by reneargento.

the class Exercise16 method readAllDates.

private static Date[] readAllDates(String fileName) {
    In in = new In(fileName);
    Queue<Date> queue = new Queue<>();
    while (!in.isEmpty()) {
        queue.enqueue(new Date(in.readString()));
    }
    int n = queue.size();
    Date[] dates = new Date[n];
    for (int i = 0; i < n; i++) {
        dates[i] = queue.dequeue();
    }
    return dates;
}
Also used : In(edu.princeton.cs.algs4.In) Queue(edu.princeton.cs.algs4.Queue) Date(edu.princeton.cs.algs4.Date)

Example 10 with Queue

use of com.jimmysun.algorithms.chapter1_3.Queue in project algorithms-sedgewick-wayne by reneargento.

the class Exercise17 method readAllTransactions.

public static Transaction[] readAllTransactions(String fileName) {
    In in = new In(fileName);
    Queue<Transaction> queue = new Queue<>();
    while (!in.isEmpty()) {
        queue.enqueue(new Transaction(in.readLine()));
    }
    int queueSize = queue.size();
    Transaction[] transactions = new Transaction[queueSize];
    for (int i = 0; i < queueSize; i++) {
        transactions[i] = queue.dequeue();
    }
    return transactions;
}
Also used : Transaction(edu.princeton.cs.algs4.Transaction) In(edu.princeton.cs.algs4.In) Queue(edu.princeton.cs.algs4.Queue)

Aggregations

In (edu.princeton.cs.algs4.In)5 Queue (edu.princeton.cs.algs4.Queue)5 Queue (chapter1.section3.Queue)4 HashSet (chapter3.section5.HashSet)2 Transaction (edu.princeton.cs.algs4.Transaction)2 StringJoiner (java.util.StringJoiner)2 UnionFind (chapter1.section5.UnionFind)1 PriorityQueueResize (chapter2.section4.PriorityQueueResize)1 SeparateChainingHashTable (chapter3.section4.SeparateChainingHashTable)1 Queue (com.jimmysun.algorithms.chapter1_3.Queue)1 Date (edu.princeton.cs.algs4.Date)1 ST (edu.princeton.cs.algs4.ST)1 StdIn (edu.princeton.cs.algs4.StdIn)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1