# CS 136 Assignment 3

This assignment consists of both a written homework component and a lab component.

# Written Homework

Problems 4.1-4.5 in Bailey. For each problem, give the worst-case time complexity using "big-Oh" notation, as well as a brief explanation.

Also be sure to look at problems 4.9-4.14. While you don't have to turn in solutions to these problems, you should be certain that you understand the answers, which can be found at the back of the text.

This homework is due Friday, February 25, in class.

# Lab Assignment

## Description

In this assignment you will implement different search algorithms and measure their performance by running a set of experiments. The assignment has two parts.

In the first part you will add two more search methods to the Java class Search that I have provided for you. Both of the methods will be similar to binary search except for the way they split the array:

• The first new method will split the array at 1/5 of its length.
• The second new method will split the array at a randomly chosen index.
Be sure to test your methods thoroughly. They should be able to handle
• finding an element at the beginning of the array
• finding an element at the end of the array
• returning -1 for an element not found
You don't need to submit these tests, but you will be graded on correctness, so it is worthwhile to test your methods thoroughly.

The second part of the assignment will require that you design and implement an experiment that will allow measuring and comparing the performance of the four methods.

In order to compare the methods fairly, you will run them all on ordered arrays. Since the position of the target in the array is the focus of our search, rather than the target itself, you can simply run your experiments on arrays containing integers 0 to n-1 (or 1 to n, if you prefer).

The experiment design is as follows:

• Apply each method to arrays of sizes 10, 20, 30, ..., 100, 200, 300, ..., 1000.
• For each size, perform 10 different searches for key values randomly selected from the range of values stored in the array (e.g., for size 100, randomly select a value in the range 0-99, and run each search method with that value as the target. Count the comparisons for each of the ten runs, and for each search method compute an average number of comparisons performed for the current problem (array) size. This average will be an approximation of the expected (average) number of comparisons required by the method for the current problem size.
• Output the averages computed in such a manner that they allow for the comparison of the four search methods considered.
Note. To make the measurements described above, you will need to add counters and counting code to your methods (and/or to the class). Do this in a manner that is as transparent as possible. A user of the Search class should be able to invoke the various methods without knowing that any counting is happening!

## Goals

The goals are to learn how to measure the performance of algorithms using experiments. You should prepare a design of your solution to this assignment for the lab on Wednesday. The design should include complete method headers for the two new searches, as well as pseudocode for the methods and the experiment component.

## Naming Conventions

As always, remember that classes requested for assignments should always be named as in the assignment. Don't forget to follow the usual conventions for naming of methods, variables, etc.

## What to hand in

The program is due Sunday, February 27 for those in a Wednesday lab, and Monday, February 28 for those in the Thursday lab. Programs are handed in by putting a folder, whose name is your last name, into the CS136 drop-off folder on Cider Press. The folder should contain the Metrowerks project folder for your Search class. Please also remember to turn in a printed copy either to me to to one of the 136 TAs.

Back to: