Search in sources :

Example 1 with Exercise21_ComparableTransactions

use of chapter2.section1.Exercise21_ComparableTransactions in project algorithms-sedgewick-wayne by reneargento.

the class Exercise33_RandomTransactions method generateRandomTransactions.

private Exercise21_ComparableTransactions[] generateRandomTransactions(int numberOfObjects) {
    Exercise21_ComparableTransactions[] transactions = new Exercise21_ComparableTransactions[numberOfObjects];
    for (int i = 0; i < numberOfObjects; i++) {
        String who = "Client " + (i + 1);
        int month = StdRandom.uniform(12) + 1;
        int maxDay = month == 2 ? 28 : 30;
        int day = StdRandom.uniform(maxDay) + 1;
        int year = StdRandom.uniform(1900, 2018);
        Date date = new Date(month, day, year);
        double amount = (double) Math.round(StdRandom.uniform(0.0, 1000000.0) * 100) / 100;
        Exercise21_ComparableTransactions transaction = new Exercise21_ComparableTransactions(who, date, amount);
        transactions[i] = transaction;
    }
    return transactions;
}
Also used : Date(edu.princeton.cs.algs4.Date) Exercise21_ComparableTransactions(chapter2.section1.Exercise21_ComparableTransactions)

Example 2 with Exercise21_ComparableTransactions

use of chapter2.section1.Exercise21_ComparableTransactions in project algorithms-sedgewick-wayne by reneargento.

the class Exercise33_RandomTransactions method doExperiment.

private void doExperiment(int numberOfObjects) {
    int[] values;
    if (numberOfObjects != 0) {
        values = new int[] { numberOfObjects };
    } else {
        values = new int[] { 1000, 10000, 100000, 1000000 };
    }
    String[] sortAlgorithms = { SHELL_SORT, MERGE_SORT, QUICK_SORT, HEAP_SORT };
    StdOut.printf("%13s %13s %13s\n", "Number of Transactions | ", "Sort Method | ", "Time Spent");
    for (int n = 0; n < values.length; n++) {
        Exercise21_ComparableTransactions[] transactions = generateRandomTransactions(values[n]);
        for (int i = 0; i < sortAlgorithms.length; i++) {
            Exercise21_ComparableTransactions[] transactionsCopy = new Exercise21_ComparableTransactions[transactions.length];
            System.arraycopy(transactions, 0, transactionsCopy, 0, transactions.length);
            Stopwatch timer = new Stopwatch();
            switch(sortAlgorithms[i]) {
                case SHELL_SORT:
                    ShellSort.shellSort(transactionsCopy);
                    break;
                case MERGE_SORT:
                    TopDownMergeSort.mergeSort(transactionsCopy);
                    break;
                case QUICK_SORT:
                    QuickSort.quickSort(transactionsCopy);
                    break;
                case HEAP_SORT:
                    HeapSort.heapSort(transactionsCopy);
                    break;
            }
            double runningTime = timer.elapsedTime();
            printResults(values[n], sortAlgorithms[i], runningTime);
        }
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch) Exercise21_ComparableTransactions(chapter2.section1.Exercise21_ComparableTransactions)

Aggregations

Exercise21_ComparableTransactions (chapter2.section1.Exercise21_ComparableTransactions)2 Date (edu.princeton.cs.algs4.Date)1 Stopwatch (edu.princeton.cs.algs4.Stopwatch)1