## CS107 - Lab 3

Given an algorithm that solves a problem, how do we measure its efficiency? How do we know that one algorithm is more efficient than another? This lab helps understand the measurement of algorithm efficiency and its importance. The (suggested) reading for this lab is (part of) Chapter 3 in the SG textbook.

### Designing and analyzing algorithms

1. Algorithms A and B perform the same task. On input of size n, algorithm A executes 0.003n2 instructions, and algorithm B executes 243n instructions. Write their efficiencies using Theta-notation. Is algorithm B more efficient than algorithm A? Find the approximate value of n above which algorithm B is more efficient .

2. At about what value of n does an algorithm that does 100 n2 instructions become more efficient than one that does 0.01x2n instructions? Use a calculator.

3. An algorithm that is Theta(n) takes 10 seconds to execute on a particular computer when n=100. How long would you expect it to take when n=500?

4. An algorithm that is Theta(n2) takes 10 seconds to execute on a particular computer when n=100. How long would you expect it to take when n=500?

5. Here is a list of seven names:
```Sherman, Jane, Ted, Elise, Raul, Maki, John
```
Search this list for each name in turn, using sequential search, and count the number of comparisons for each name (Note: in class we counted the total number of instructions; for simplicity, in this problems you count comparisons). Now take the seven comparison counts and find their average. Use this to find the (approximate) average number of comparisons needed to find a name in an n-element list, and briefly explain why.

6. If a list is already sorted in increasing order, a modified sequential search algorithm can be used that compares against each element in turn, stopping if a list element exceeds the target value. Write a pseudocode version of this short sequential search.

7. What is the worst-case number of comparisons of the short sequential search algorithm above on a sorted list of n elements?

8. Assume that we search once for each element in sorted input list using the short sequential search algorithm above. What is the approximate average number of comparisons to find an element?

9. Is the short sequential search algorithm above ever more efficient than regular sequential search? Explain.

10. Consider the following problem (the bin-packing problem): Suppose we have an unlimited number of bins of volume 1 unit, and n objects, all of volume between 0.0 and 1.0. We would like to find the minimum number of bins needed to store the n objects. A solution algorithm for this problem would be of interest to any manufacturer who ships sets of various items in standard-sized cartons, or to anyone who wants to store image files on a set of CDs in the most efficient way. Sketch an algorithm to solve this problem. For this particular problem we do not care so much about writing the algorithm in (correct) pseudocode, but rather on what the idea of the algorithm is. You can describe your algorithm in a paragraph if you prefer, rather than use pseudocode.

### What to turn in:

• The 10 problems in the Designing Algorithms section.

Be sure to justify your answers! You may do this work either with a word processor or by hand. If you type your solutions, bring a hard copy of your assignment and hand it in class. If you write by hand, do your best to write legibly and leave space between problems.

You may choose to do this assignment either by yourself or in a group. However, solutions should be written up individually and handed in on the due date, at the beginning of class.

Once you are finished in the lab, if you want to save any documents, you'll have to drag them to your network folder (mounted to your desktop). Files left on the desktop and in your local folder (on the local machine) will be erased!