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;
}
}
}
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;
}
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();
}
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;
}
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;
}
Aggregations