Search in sources :

Example 21 with Stopwatch

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

the class Exercise31_HistogramOfRunningTimes method doExperiment.

private static void doExperiment(int numberOfExperiments, Map<Integer, Comparable[]> allInputArrays) {
    List<Double> runningTimes = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_ARRAYS; i++) {
        for (int j = 0; j < numberOfExperiments; j++) {
            Comparable[] originalArray = allInputArrays.get(i);
            Comparable[] array = new Comparable[originalArray.length];
            System.arraycopy(originalArray, 0, array, 0, originalArray.length);
            Stopwatch timer = new Stopwatch();
            QuickSort.quickSort(array);
            double runningTime = timer.elapsedTime();
            runningTimes.add(runningTime);
        }
    }
    histogram(runningTimes);
}
Also used : ArrayList(java.util.ArrayList) Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 22 with Stopwatch

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

the class Exercise39_ActualTimings method frequencyCounter.

private String frequencyCounter(SymbolTable<String, Integer> symbolTable, String[] words, int minLength, String title) {
    String xAxisLabel = "calls to get() or put()";
    String yAxisLabel = "running time";
    double maxNumberOfOperations = 45000;
    double maxRunningTime = 3000;
    int originValue = 0;
    VisualAccumulator visualAccumulator = new VisualAccumulator(originValue, maxNumberOfOperations, maxRunningTime, title, xAxisLabel, yAxisLabel);
    double totalRunningTime = 0;
    Stopwatch timer;
    for (String word : words) {
        if (word.length() < minLength) {
            continue;
        }
        if (!symbolTable.contains(word)) {
            timer = new Stopwatch();
            symbolTable.put(word, 1);
            totalRunningTime += timer.elapsedTime() * 1000;
            visualAccumulator.addDataValue(totalRunningTime, false);
        } else {
            timer = new Stopwatch();
            int wordFrequency = symbolTable.get(word);
            totalRunningTime += timer.elapsedTime() * 1000;
            visualAccumulator.addDataValue(totalRunningTime, false);
            timer = new Stopwatch();
            symbolTable.put(word, wordFrequency + 1);
            totalRunningTime += timer.elapsedTime() * 1000;
            visualAccumulator.addDataValue(totalRunningTime, false);
        }
    }
    String max = "";
    timer = new Stopwatch();
    symbolTable.put(max, 0);
    totalRunningTime += timer.elapsedTime() * 1000;
    visualAccumulator.addDataValue(totalRunningTime, false);
    for (String word : symbolTable.keys()) {
        timer = new Stopwatch();
        int wordFrequency = symbolTable.get(word);
        totalRunningTime += timer.elapsedTime() * 1000;
        visualAccumulator.addDataValue(totalRunningTime, false);
        timer = new Stopwatch();
        int maxWordFrequency = symbolTable.get(max);
        totalRunningTime += timer.elapsedTime() * 1000;
        visualAccumulator.addDataValue(totalRunningTime, false);
        if (wordFrequency > maxWordFrequency) {
            max = word;
        }
    }
    timer = new Stopwatch();
    int maxFrequency = symbolTable.get(max);
    totalRunningTime += timer.elapsedTime() * 1000;
    visualAccumulator.addDataValue(totalRunningTime, false);
    visualAccumulator.writeLastComputedValue();
    return max + " " + maxFrequency;
}
Also used : VisualAccumulator(util.VisualAccumulator) Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 23 with Stopwatch

use of edu.princeton.cs.algs4.Stopwatch 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)

Example 24 with Stopwatch

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

the class Exercise24_Samplesort method doExperiment.

private static void doExperiment(int numberOfExperiments, int initialArraySize, int sampleSize, Map<Integer, Comparable[]> allInputArrays) {
    StdOut.printf("%13s %23s %25s\n", "Array Size | ", "QuickSort Running Time |", "SampleSort Running Time");
    int arraySize = initialArraySize;
    for (int i = 0; i < numberOfExperiments; i++) {
        Comparable[] originalArray = allInputArrays.get(i);
        Comparable[] array = new Comparable[originalArray.length];
        System.arraycopy(originalArray, 0, array, 0, originalArray.length);
        // Default QuickSort
        Stopwatch quickSortTimer = new Stopwatch();
        QuickSort.quickSort(originalArray);
        double quickSortRunningTime = quickSortTimer.elapsedTime();
        // SampleSort
        Stopwatch sampleSortTimer = new Stopwatch();
        sampleSort(array, sampleSize);
        double sampleSortRunningTime = sampleSortTimer.elapsedTime();
        printResults(arraySize, quickSortRunningTime, sampleSortRunningTime);
        arraySize *= 2;
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 25 with Stopwatch

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

the class Exercise25_CutoffToInsertionSort method doExperiment.

private static void doExperiment(Map<Integer, Comparable[]> allInputArrays) {
    StdOut.printf("%13s %23s\n", "Cutoff Size | ", "Average Running Time");
    for (int cutoffSize = 0; cutoffSize <= 30; cutoffSize++) {
        double totalRunningTime = 0;
        for (int i = 0; i < NUMBER_OF_EXPERIMENTS; i++) {
            Comparable[] originalArray = allInputArrays.get(i);
            Comparable[] array = new Comparable[originalArray.length];
            System.arraycopy(originalArray, 0, array, 0, originalArray.length);
            Stopwatch timer = new Stopwatch();
            QuickSortWithCutoff.quickSortWithCutoff(array, cutoffSize);
            totalRunningTime += timer.elapsedTime();
        }
        printResults(cutoffSize, totalRunningTime / NUMBER_OF_EXPERIMENTS);
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Aggregations

Stopwatch (edu.princeton.cs.algs4.Stopwatch)62 ArrayList (java.util.ArrayList)5 In (edu.princeton.cs.algs4.In)2 VisualAccumulator (util.VisualAccumulator)2 Exercise21_ComparableTransactions (chapter2.section1.Exercise21_ComparableTransactions)1 RedBlackBST (chapter3.section3.RedBlackBST)1 LinearProbingHashTable (chapter3.section4.LinearProbingHashTable)1