Search in sources :

Example 46 with Stopwatch

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

the class Exercise20_NonrecursiveQuicksort method doExperiment.

private static void doExperiment(int numberOfExperiments, int initialArraySize, Map<Integer, Comparable[]> allInputArrays) {
    StdOut.printf("%13s %23s %22s\n", "Array Size | ", "QuickSort Running Time |", "Nonrecursive QuickSort");
    int arraySize = initialArraySize;
    for (int i = 0; i < numberOfExperiments; i++) {
        Comparable[] originalArray = allInputArrays.get(i);
        Comparable[] arrayCopy1 = new Comparable[originalArray.length];
        System.arraycopy(originalArray, 0, arrayCopy1, 0, originalArray.length);
        // Default QuickSort
        Stopwatch defaultQuickSortTimer = new Stopwatch();
        QuickSort.quickSort(originalArray);
        double defaultQuickSortRunningTime = defaultQuickSortTimer.elapsedTime();
        // Nonrecursive QuickSort
        Stopwatch nonRecursiveQuickSortTimer = new Stopwatch();
        nonRecursiveQuickSort(arrayCopy1);
        double nonRecursiveQuickSortRunningTime = nonRecursiveQuickSortTimer.elapsedTime();
        printResults(arraySize, defaultQuickSortRunningTime, nonRecursiveQuickSortRunningTime);
        arraySize *= 2;
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 47 with Stopwatch

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

the class Exercise22_Fast3WayPartitioning method doExperiment.

private static void doExperiment(int numberOfExperiments, int initialArraySize, Map<Integer, Comparable[]> allInputArrays) {
    StdOut.printf("%13s %16s %38s\n", "Array Size | ", "QuickSort 3-Way |", "QuickSort with fast 3-way partitioning");
    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);
        // QuickSort 3-Way
        Stopwatch quickSort3WaySortTimer = new Stopwatch();
        QuickSort3Way.quickSort3Way(originalArray);
        double quickSort3WayRunningTime = quickSort3WaySortTimer.elapsedTime();
        // QuickSort with fast 3-way partitioning (Bentley-McIlroy)
        Stopwatch quickSortWithFast3WayPartitioning = new Stopwatch();
        quickSortWithFast3WayPartitioning(array);
        double quickSortWithFast3WayPartitioningRunningTime = quickSortWithFast3WayPartitioning.elapsedTime();
        printResults(arraySize, quickSort3WayRunningTime, quickSortWithFast3WayPartitioningRunningTime);
        arraySize *= 2;
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 48 with Stopwatch

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

the class Exercise23_TukeysNinther method doExperiment.

private static void doExperiment(int numberOfExperiments, int initialArraySize, Map<Integer, Comparable[]> allInputArrays) {
    StdOut.printf("%13s %16s %38s %52s\n", "Array Size | ", "QuickSort 3-Way |", "QuickSort with fast 3-way partitioning | ", "QuickSort w/ fast 3-way partitioning + Tukey Ninther");
    int arraySize = initialArraySize;
    for (int i = 0; i < numberOfExperiments; 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 3-Way
        Stopwatch quickSort3WaySortTimer = new Stopwatch();
        QuickSort3Way.quickSort3Way(originalArray);
        double quickSort3WayRunningTime = quickSort3WaySortTimer.elapsedTime();
        // QuickSort with fast 3-way partitioning (Bentley-McIlroy)
        Stopwatch quickSortWithFast3WayPartitioning = new Stopwatch();
        Exercise22_Fast3WayPartitioning.quickSortWithFast3WayPartitioning(arrayCopy1);
        double quickSortWithFast3WayPartitioningRunningTime = quickSortWithFast3WayPartitioning.elapsedTime();
        // QuickSort with fast 3-way partitioning (Bentley-McIlroy) + Tukey Ninther
        Stopwatch quickSortWithFast3WayPartitioningTukeyNinther = new Stopwatch();
        quickSortWithFast3WayPartitioningTukeyNinther(arrayCopy2);
        double quickSortWithFast3WayPartitioningTukeyNintherRunningTime = quickSortWithFast3WayPartitioningTukeyNinther.elapsedTime();
        printResults(arraySize, quickSort3WayRunningTime, quickSortWithFast3WayPartitioningRunningTime, quickSortWithFast3WayPartitioningTukeyNintherRunningTime);
        arraySize *= 2;
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 49 with Stopwatch

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

the class Exercise28_SelectionFilter method doExperimentToEstimateRunningTime.

private static void doExperimentToEstimateRunningTime(int arraySize, int numberOfExperiments) {
    // 10^4
    int m = 10000;
    for (int i = 0; i < numberOfExperiments; i++) {
        PriorityQueue<Point> priorityQueue = new PriorityQueue<>(m + 1, PriorityQueue.Orientation.MAX);
        Point[] pointArray = generateRandomPointsArray(arraySize);
        Stopwatch timer = new Stopwatch();
        for (Point point : pointArray) {
            priorityQueue.insert(point);
            if (priorityQueue.size() > m) {
                priorityQueue.deleteTop();
            }
        }
        Stack<Point> pointsStack = reversePointsOrder(priorityQueue);
        printPoints(pointsStack);
        double runningTime = timer.elapsedTime();
        StdOut.println("Running time for N = " + arraySize + " and M = " + m + ": " + runningTime);
        arraySize *= 10;
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch)

Example 50 with Stopwatch

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

the class Exercise29_ShellsortIncrements method time.

public static double time(Comparable[] array, int[] incrementSequence) {
    Stopwatch timer = new Stopwatch();
    shellsort(array, incrementSequence);
    return timer.elapsedTime();
}
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