CSCI 132 Data Structures--Spring 2014

    Home | | Syllabus | | Assignments | | Lecture Notes

    Laboratory 8 Solutions

    Solutions to programs:

    searchCompare.cc

    Problem 2:
    a. Average number of comparisons for n = 5, 10, 20, 100, 1000

    Successful:n = 5n = 10n = 20n = 100n = 1000
    Sequential3.235.9711.4355.17547.31
    Binary 13.414.425.377.6410.94
    Binary 23.55.126.3410.7817.04
    Unsuccessful:
    Sequential510201001000
    Binary 13.364.385.367.6810.98
    Binary 25.367.129.1413.719.96

    b) Explain your results for a for a list length of 100 based on the formulas for the running time of each of the search algorithms given in the text.

    The results match what would be expected from the formulas fairly well. They are a little different from the exact predicted values (as might be expected due to the randomized testing). The following are the expected and computed numbers for n = 100:

    SuccessfulComputedExpectedFormula
    Sequential55.1750.5(n+1)/2
    Binary 17.647.64lg(n) + 1
    Binary 210.7810.282lg(n) - 3
    UnsuccessfulComputedExpectedFormula
    Sequential100100n
    Binary 17.687.64lg(n) + 1
    Binary 213.713.282lg(n)

    c) For what values of n does sequential search do fewer average comparisons than binary_search_1?

      Successful searches: Sequential search does fewer comparisons for n = 2, 3, 4, and 5.

      Unsuccessful searches: Sequential search does fewer comparisons for no values of n.

    d) For what values of n does sequential search do fewer average comparisons than binary_search_2?

      Successful searches: Sequential search does fewer comparisons for n = 2, 3, 4, 5, and 6.

      Unsuccessful searches: Sequential search does fewer comparisons for n = 1, 2, 3, 4, 5.

    e) For what values of n does binary_search_2 do fewer average comparisons than binary_search_1?

      Successful searches: Binary 2 does fewer comparisons for n = 3 and 4.

      Unsuccessful searches: Binary 2 does fewer comparisons for no values of n.

    f) Draw the comparison trees for binary_search_1 and binary_search_2 for n = 7.


    Home | | Syllabus | | Assignments | | Lecture Notes