## CS107 - Lab 4

### Introduction to algorithmic problem solving (part 4: efficiency)

In this lab you will continue to work on algorithms, and in addition think about efficiency.

Work individually, and call me if you need help. You are encouraged to discuss ideas and techniques broadly with other class members, but not specifics. Discussions should be limited to questions that can be asked and answered without using any written medium (e.g. pencil and paper or email). You should at no point look at the screen of your colleagues.

1. Draw the tree structure that describes binary search on a list with 16 elements. What is the number of comparisons in the worst case?

2. Assume we are using binary search to search for a target that is smaller than all elements in a (sorted) list. What happens to the variable start throughout the algorithm? What values does it take? What about the variable end?

3. You know by now that binary search is more efficient than sequential search (that is, if count only the search part, ignoring reading the input). However, in order for binary search to work, the input list must be sorted. In this problem you will explore the tradeoff between using sequential search on an unsorted list, as opposed to sorting the list and then using binary search.

Assume we use a sorting algorithm that performs n2 instructions in the worst case.

If we search only once, it is clear that is better to use sequential search which is order of n, than sort the list then use binary search, which is order of n2 + lg n in total.

The question is, what happens if we perform more than one search. Note that, in order to use binary search, we only need to sort the input once. Thus, as we perform more searches on the same list, the second approach may become faster. This is because we only need to sort the list once; thus, after the first binary search (which has to sort the list), the subsequent ones will use only lg n in the worst case.

If the list size is n=100,000, about how many searches must be done before the second alternative (sorting plus binary search) is better?

4. In this problem you will develop an algorithm that evaluates a polynomial at a point. A polynomial is a generalization of a linear function ax+b, but now the degree of x can be arbitrary: P(x) = a0 + a1x+a2x2 ...+anxn. In this problem we will assume n = 50.

First, your algorithm should read in from the user a list of n+1 numbers, a0, a1, a2,...,an (these are called coefficients).

Then it should ask the user for a value x. Given the values of the coefficients and x, your algorithm should compute and print the value a0 + a1x+a2x2 ...+anxn.

There is no instruction that computes powers of numbers. As part of your algorithm you will need to compute the product xn; that is, you cannot assume that you can use xn in pseudocode.

Analyze the worst-case and best-case running time of your algorithm (the order of magnitude, or theta-notation is enough).

5. Consider the following algorithm for sorting: pick the largest element of the list and place it in the last position; then repeat on the remaining elements. This is referred to as SelectionSort. The algorithm, at a high level of abstraction, is given bellow:
```SelectionSort
get a1,...an
set unsortedEnd = n
while (unsortedEnd > 1)
find the position of the largest element among a1, a2,..,aunsortedEnd
assign this position to p
swap ap with aunsortedEnd
unsortedEnd = unsortedEnd - 1
print "The list in sorted order is" a1, a2,..,an
```
1. Show how the algorithm works if the input list is 3,1,2,5,4
2. Describe in words what happens in the first iteration of the repeat loop (one sentence).
3. Describe in words what happens in the second iteration of the repeat loop (one sentence).
4. How many times is the repeat loop executed for a list of size n?
5. For each of the 4 steps within the repeat loop, write down how many instructions they take in the first iteration of the loop,for a list of size n.
6. Same, for the second iteration of the loop.
7. Same, for the last iteration of the loop.
8. By adding up the numbers that you got above, find out the total running time (number of instructions) of SelectionSort, as a function of n, for a list of size n.

6. Write an algorithm that reads a number n and a list of n-1 distinct integers from the user in the range 1,2,...,n and figures out what is the missing number. For instance, if the user types in
```6
1 5 3 2 6
```
your algorithm should print
```The missing number is 4
```

Analyze the worst-case and best-case running time of your algorithm (the order of magnitude, or theta-notation is enough).

7. Write an algorithm that reads a number n and a list of n numbers from a user and computes and prints the median element(s) in the list (the median is an element which has an equal number of elements smaller and larger than it). For instance for the list
` 4 3 1 2 5 7  6`
the median is 4. For the list
` 5 4 3 8 6 7 1 2`
the medians are 4 and 5.

You can use any of the algorithms studied in class as a black box, without writing the code for them.

Analyze the worst-case and best-case running time of your algorithm (the order of magnitude, or theta-notation is enough).

8. Write an algorithm that asks the user for a number n, and prints the following pattern on the screen. The example below is for n=10, that is, the longest line has 10 stars.
```*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*
Goodbye.
```
Assume that subsequent print instructions all print on the same line. To start a new line, you will need to issus a special instruction, which we'll call print newline. For example:
```print "a"
print "b"
print "c"
```
will print
```abc
```
while
```print newline
print "a"
print newline
print "b"
print newline
print "c"
```
will print
```a
b
c
```