Laboratory: Run-Time Experiments for Searching Algorithms
CSC 105 The Digital Age Spring 2009

Elements of Efficiency

Although computers are often considered to work very quickly, only some algorithms proceed rapidly; others take much longer. This laboratory exercise provides an intuitive framework for the consideration of algorithm effectiveness, including the amount of time and memory required for an algorithm. When algorithms are applied to successively larger data sets, some algorithms may scale up nicely while others require more time than may be feasible. Also, when several algorithms are available to solve a problem, it is natural to wonder if one solution is better than another. Altogether, we might use many criteria to evaluate such solutions, including:

For this laboratory exercise, we focus on algorithm execution time.

Algorithm Execution Time

In determining algorithm execution time, we may proceed in several ways:

Each of these approaches has advantages, but each also has drawbacks. Execution times on a specific machine normally depend upon details of the machine and on the specific data used. Timings may vary from data set to data set and from machine to machine, so experiments from one machine and one data set may not be very helpful in general.

For many purposes, it turns out that a high-level analysis provides adequate information to compare algorithms. For the most part, we follow that approach here.

Linear and Binary Searches

In class, we have discussed both the linear search and the binary search.

  1. Write a brief description (a few sentences) of how a linear search works.

  2. Write a brief description (a few sentences) of how a binary search works. Be sure to include any assumptions the algorithm makes.

In class, we have discussed how efficient each of these algorithms might be. Now we take a more concrete, experimental approach.

Experiments Regarding Search

We will search for random numbers in a sequence of even integers:

0, 2, 4, 6, ..., maximum

Technically, the structure holding such a collection of data is called an array. With the array data just described, a search should be successful if we are looking for an even, nonnegative integer that is not too large. The search should fail if the desired integer is negative, odd, or too large. In our experiment, we will pick 60 integers as candidates for the search. We pick the first 30 from within the array, to ensure that the search will be successful. Then we then pick the last 30 from the set of odd integers, so that the search will fail. In each case, we will select numbers at random from the given set.

The searchTest program

The program searchTest performs both a linear search and a binary search for randomly-selected items, and records the (approximate) number of comparisons performed by each search algorithm.

The mechanics of running these search experiments are as follows:

Collecting Experimental Data

In this part of the lab, you will gather experimental data regarding the search times for both linear and binary searches, using various array sizes.
  1. Run searchTest for a several array sizes between 1000 and 50,000.

    Use a spreadsheet to record the results from each program run (i.e., for each array size in your experiment). I suggest you make separate tables to record each of the following:

    1. array sizes and steps for successful searches with linear search,
    2. array sizes and steps for unsuccessful searches with linear search,
    3. array sizes and steps for successful searches using binary search, and
    4. array sizes and steps for unsuccessful searches using binary search.

    Do double-check your data entry! Typographical errors can add more spice than is intended to your results.

  2. Now use your spreadsheet to plot the data for each of the four tables you just created. (Make a separate graph for each table.) The horizontal axis should indicate the array size, and the vertical axis should indicate the average number of steps required. You should conduct sufficient experiments, so that a fairly consistent pattern emerges.

    Recall that, in OpenOffice Calc you can create a graph by selecting Chart... from the Insert menu. You will probably find that an "XY Chart" is appropriate for this type of data. (Its icon has several squares scattered around the graph.) Once you have selected that, you may also find the subtype called "Symbols Only" to be appropriate.

    Finally, I suggest that you use the same y-axis scale for each pair of graphs that depict results from the same search algorithm. This will make a visual comparison of the graphs more meaningful. (In OpenOffice Calc, you can set the y-axis scale as follows. Double-click on the chart, then right-click and select "axis" and then "y-axis" from the menu.)

  3. Describe some conclusions you can draw from your experiments. For example, you might compare and contrast the various graphs you have made.

Created: Henry Walker, December 31, 2003
Revised: Henry Walker, February 27, 2006
Marge Coahran, March 14, 2007
Marge Coahran, April 3, 2008
Jerod Weinman, January 2, 2009
Adopted from Run-Time Experiments