The analysis of instructions may take into account the nature of the data -- for example, one might consider what happens in a worst case. Also, such analysis commonly is based on the size of the data being processed -- the number of items or how large or small the data are. This is sometimes called a microanalysis of program execution. Once again, however, the specific instructions may vary from machine to machine, and detailed conclusions from one machine may not apply to another.
A high-level analysis may identify types of activities performed, without considering exact timings of instructions. This is sometimes called a macroanalysis of program execution. This can give a helpful overall assessment of an algorithm, based on the size of the data. However, such an analysis cannot show fine variations among algorithms or machines.
For many purposes, it turns out than a high-level analysis provides adequate information to compare algorithms. For the most part, we follow that approach here.
In class, we have discussed both the linear search and the binary search.
Write a paragraph or two description of how a linear search works.
Write a paragraph or two description of how a binary search works.
In class, we have discussed how efficient each of these algorithms might be. Now we take a more concrete, experimental approach.
We will search for random items in a sequence of even integers:
Techically, 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 10 integers at random as candidates for the search.
The program searchTest performs both a linear search and a binary search for randomly-selected items, and records the time required for this work. Due to the speed of the computer and the limitations of the clocking mechanism available, we repeat each experiment 60,000 times. This means that times are magnified, so we can easily see differences.
The mechanics of running these search experiments are as follows:
Log onto a computer in MathLAN, and open a terminal window.
In the terminal window, type the command:
cd ~walker/105/labs
The program itself can be run with the command
/opt/IBMJava2-141/bin/java searchTest
The program will ask you to enter the size of the sequence to be searched. Suggested values might be selected from the range from 1000 to 50,000, although you can choose smaller or larger sizes for the array. The program then will report the time required to search for each of 10 values in the array, both for the linear search and for the binary search.
Note: After you run the above command the first time, you can use the upward-arrow key at your keyboard to retrieve the same command again. After hitting the upward-arrow key to get the desired command, hit Return or Enter to run the program again.
In this part of the lab, you are to gather experimental data regarding the search times for various size arrays for both the linear and binary searches. Since the searchTest program performs 10 trials, you will need to combine the results in some way. For example, you might want to average the results or you might choose a median value.
Run searchTest for a variety of array sizes between 1000 and 50,000, recording your results in each case.
Plot the combined values (averages or medians) of your various trials carefully on graph paper. The horizontal axis on the graph paper should indicate the size of the array, and the vertial axis should indicate time. You should conduct sufficient experiments, so that a fairly consistent pattern emerges.
Draw a reasonable graph through your data points to suggest the overall approximate relationship between the size of the array and the time required for a typical search.
Describe (in words) the nature of the graphs you have observed.
Your experimental results, of course, relied on a particular program running on a specific machine. Actual numbers are likely to vary from one computer and program to another. However, we still would anticipate the same general patterns -- even if the numbers differ.
The following table gives experimental measurements for the average time required for a linear search for several search trials on another machine with another program.
| Array | Average Time | Average Time If |
|---|---|---|
| Size | If Value Found | Value Not Found |
| 1000 | 620 | 1248 |
| 2000 | 1260 | 2490 |
| 4000 | 2540 | 4960 |
Estimate the time for an average linear search of arrays of size 1500, 3000, 8000, and 16000. Briefly justify your answers.
Continuing this experiment, the following table gives experimental measurements for the average time required for a binary search for several search trials.
| Array | Average Time | Average Time If |
|---|---|---|
| Size | If Value Found | Value Not Found |
| 1000 | 33 | 33 |
| 2000 | 37 | 37 |
| 4000 | 41 | 41 |
Estimate the time for an average binary search of arrays of size 1500, 3000, 8000, and 16000. Briefly justify your answers.
|
created December 31, 2003 last revised February 18, 2003
|
|