Search in sources :

Example 1 with LinearProbingHashTable

use of chapter3.section4.LinearProbingHashTable in project algorithms-sedgewick-wayne by reneargento.

the class Exercise35_PrimitiveTypes method doExperiment.

private void doExperiment(int[] numberOfSizesAndQueries, int[] randomIntKeys, double[] randomDoubleKeys, int[] queriesInt, double[] queriesDouble) {
    StdOut.printf("%17s %12s %20s %10s\n", "Symbol table | ", "Key type | ", "Size and queries | ", "Time spent");
    // Linear probing hash table with Integer keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        LinearProbingHashTable<Integer, Integer> linearProbingHashTableInteger = new LinearProbingHashTable<>(997);
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableInteger.put(randomIntKeys[j], randomIntKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableInteger.get(queriesInt[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(LINEAR_PROBING_SYMBOL_TABLE_TYPE, INTEGER_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Linear probing hash table with int keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        Exercise4.HashSTint<Integer> linearProbingHashTableint = new Exercise4().new HashSTint<>(997);
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableint.put(randomIntKeys[j], randomIntKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableint.get(queriesInt[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(LINEAR_PROBING_SYMBOL_TABLE_TYPE, PRIMITIVE_INT_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Linear probing hash table with Double keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        LinearProbingHashTable<Double, Double> linearProbingHashTableDouble = new LinearProbingHashTable<>(997);
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableDouble.put(randomDoubleKeys[j], randomDoubleKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableDouble.get(queriesDouble[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(LINEAR_PROBING_SYMBOL_TABLE_TYPE, DOUBLE_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Linear probing hash table with double keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        Exercise4.HashSTdouble<Double> linearProbingHashTabledouble = new Exercise4().new HashSTdouble<>(997);
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTabledouble.put(randomDoubleKeys[j], randomDoubleKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTabledouble.get(queriesDouble[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(LINEAR_PROBING_SYMBOL_TABLE_TYPE, PRIMITIVE_DOUBLE_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Red-black BST with Integer keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        RedBlackBST<Integer, Integer> redBlackBSTInteger = new RedBlackBST<>();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTInteger.put(randomIntKeys[j], randomIntKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTInteger.get(queriesInt[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(RED_BLACK_BST_SYMBOL_TABLE_TYPE, INTEGER_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Red-black BST with int keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        Exercise5.STint<Integer> redBlackBSTint = new Exercise5().new STint<>();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTint.put(randomIntKeys[j], randomIntKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTint.get(queriesInt[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(RED_BLACK_BST_SYMBOL_TABLE_TYPE, PRIMITIVE_INT_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Red-black BST with Double keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        RedBlackBST<Double, Double> redBlackBSTDouble = new RedBlackBST<>();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTDouble.put(randomDoubleKeys[j], randomDoubleKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTDouble.get(queriesDouble[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(RED_BLACK_BST_SYMBOL_TABLE_TYPE, DOUBLE_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Red-black BST with double keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        Exercise5.STdouble<Double> redBlackBSTdouble = new Exercise5().new STdouble<>();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTdouble.put(randomDoubleKeys[j], randomDoubleKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTdouble.get(queriesDouble[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(RED_BLACK_BST_SYMBOL_TABLE_TYPE, PRIMITIVE_DOUBLE_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch) LinearProbingHashTable(chapter3.section4.LinearProbingHashTable) RedBlackBST(chapter3.section3.RedBlackBST)

Aggregations

RedBlackBST (chapter3.section3.RedBlackBST)1 LinearProbingHashTable (chapter3.section4.LinearProbingHashTable)1 Stopwatch (edu.princeton.cs.algs4.Stopwatch)1