Search in sources :

Example 51 with Stopwatch

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

the class Exercise29_Randomization method doExperiment.

private static void doExperiment(Map<Integer, Comparable[]> allInputArrays) {
    int[] cutoffSizes = { 10, 20, 50 };
    StdOut.printf("%13s %13s %30s %23s\n", "Cutoff Size | ", "Array Size | ", "QuickSort W/ Randomized Array Running Time |", "QuickSort W/ Random Pivot");
    for (int cutoffSize : cutoffSizes) {
        for (int i = 0; i < NUMBER_OF_EXPERIMENTS; i++) {
            Comparable[] originalArray = allInputArrays.get(i);
            Comparable[] arrayCopy1 = new Comparable[originalArray.length];
            System.arraycopy(originalArray, 0, arrayCopy1, 0, originalArray.length);
            Comparable[] arrayCopy2 = new Comparable[originalArray.length];
            System.arraycopy(originalArray, 0, arrayCopy2, 0, originalArray.length);
            // QuickSort with cutoff and randomized array
            Stopwatch quickSortWithRandomizedArrayTimer = new Stopwatch();
            QuickSortWithCutoff.quickSortWithCutoff(arrayCopy1, cutoffSize);
            double quickSortWithRandomizedArrayRunningTime = quickSortWithRandomizedArrayTimer.elapsedTime();
            // QuickSort with cutoff and choosing a random pivot
            Stopwatch quickSortWithRandomPivotTimer = new Stopwatch();
            quickSortWithCutoffAndRandomPivot(arrayCopy2, cutoffSize);
            double quickSortWithRandomPivotRunningTime = quickSortWithRandomPivotTimer.elapsedTime();
            printResults(cutoffSize, originalArray.length, quickSortWithRandomizedArrayRunningTime, quickSortWithRandomPivotRunningTime);
        }
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 52 with Stopwatch

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

the class Exercise23_CompareQFandQUErdosRenyi method doExperiments.

private void doExperiments(int numberOfExperiments) {
    List<Exercise23_CompareQFandQUErdosRenyi.Experiment> experiments = new ArrayList<>();
    int numberOfSites = 512;
    for (int i = 0; i < numberOfExperiments; i++) {
        // QuickUnion
        UF quickUnion = new QuickUnion(numberOfSites);
        Stopwatch timer = new Stopwatch();
        List<Exercise18_RandomGridGenerator.Connection> connectionsGenerated = erdosRenyiGeneratingConnections(numberOfSites, quickUnion);
        double runningTimeQuickUnion = timer.elapsedTime();
        // QuickFind
        UF quickFind = new QuickUnion(numberOfSites);
        timer = new Stopwatch();
        erdosRenyiUsingConnections(quickFind, connectionsGenerated);
        double runningTimeQuickFind = timer.elapsedTime();
        Experiment experiment = new Experiment(numberOfSites, runningTimeQuickFind / runningTimeQuickUnion);
        experiments.add(experiment);
        numberOfSites *= 2;
    }
    printResults(experiments);
}
Also used : ArrayList(java.util.ArrayList) Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 53 with Stopwatch

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

the class Exercise28_TopDownVSBottomUp method time.

public static double time(MergeSortType mergeSortType, Comparable[] originalArray) {
    Comparable[] copyArray = new Comparable[originalArray.length];
    System.arraycopy(originalArray, 0, copyArray, 0, originalArray.length);
    Stopwatch timer = new Stopwatch();
    if (mergeSortType == MergeSortType.TOP_DOWN) {
        TopDownMergeSort.mergeSort(copyArray);
    } else if (mergeSortType == MergeSortType.BOTTOM_UP) {
        BottomUpMergeSort.mergeSort(copyArray);
    }
    return timer.elapsedTime();
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 54 with Stopwatch

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

the class Exercise11 method main.

public static void main(String[] args) {
    StdOut.printf("%13s %12s %6s\n", "Array Size | ", "Running Time | ", "Ratio");
    int arraySize = 8000;
    Comparable[] initialArray = ArrayGenerator.generateRandomArrayWith3Values(arraySize / 2);
    Stopwatch initialTimer = new Stopwatch();
    quickSort(initialArray);
    double previousRunningTime = initialTimer.elapsedTime();
    for (int i = 0; i < 6; i++) {
        Comparable[] array = ArrayGenerator.generateRandomArrayWith3Values(arraySize);
        Stopwatch timer = new Stopwatch();
        quickSort(array);
        double runningTime = timer.elapsedTime();
        double runningTimeRatio = runningTime / previousRunningTime;
        printResults(arraySize, runningTime, runningTimeRatio);
        arraySize *= 2;
        previousRunningTime = runningTime;
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 55 with Stopwatch

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

the class Exercise25_MultiwayMergesort method doExperiment.

private void doExperiment(int numberOfExperiments, int initialArraySize, Map<Integer, Comparable[]> allInputArrays, int subArraysToMerge) {
    StdOut.printf("%13s %12s\n", "Array Size | ", "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);
        Stopwatch timer = new Stopwatch();
        kWayMergeSort(array, subArraysToMerge);
        double runningTime = timer.elapsedTime();
        printResults(arraySize, runningTime);
        arraySize *= 2;
    }
}
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