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

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:

Opening a Spreadsheet

  1. Launch the Open Office program. Recall you can do this by clicking on the icon with the blue circle and two white birds on it in your taskbar at the bottom of the screen.

    A window should appear with a dialog box asking what type of new document you would like to create. Click the "Spreadsheet" option. Recall that information in a spreadsheet is arranged in cells. Rows are labeled by numbers (on the left hand margin), and columns are labeled by letters (at the top margin). Cells (or rectangular blocks) then are labeled by a column/row combination.

  2. Spreadsheets allow you to enter information by clicking your mouse on any cell and typing data. Click on cell A1. Enter the text "Size", which will indicate the size of the array that will be searched.

    Click on cell B1. Enter the text "Steps", which will indicate the number of steps that the search required.

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 (at least 10) array sizes between 1000 and 50,000.

    Use your spreadsheet to record the results from each program run (i.e., for each array size in your experiment). You will need to record:

    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.
    To do this, place the sizes and steps for (i) in columns A and B, respectively, the sizes and steps for (ii) in columns C and D, respectively, the sizes and steps for (iii) in columns E and F, respectively, and the sizes and steps for (iv) in columns G and H, respectively.

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

  2. Now we will the data use your spreadsheet to plot the data for each of the four tables you just created by making a separate graph for each table.

    We will say exactly how to do this in just a moment. To get a sense of how the search algorithm performance varies with array size, The horizontal axis of each of your graphs 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.

    Select two of the columns corresponding to the data for one type of search (e.g., columns A and B). In OpenOffice Calc you can create a graph from this data by selecting Chart... from the Insert menu. You will probably find that a "XY (Scatter)" chart is appropriate for this type of data. Once you have selected that, you may also find the subtype called "Points and Lines" to be appropriate. You should also check "Sort by X values."

    As you click through the options with the "Next>>" button, be sure to label the X and Y axes appropriately and title the graph indicating the search algorithm (e.g., linear or binary) and whether the data is for successful or unsuccessful searches.

    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 click on the "Format" menu, followed by the "Axis" entry, and finally "Y-axis" from the menu. In the dialogue box that appears, you can uncheck the "Automatic" selection and set both graphs of the search type to the same to the bound.

  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
Jerod Weinman, March 15, 2011
Adopted from Run-Time Experiments