**Overview**:

Some of the most important algorithms used in computing are in the
category "search and sort." These are useful in a variety of settings, such
as web search, alphabetic sorting of a list of names, etc. They are also
related, since the most efficient search algorithms occur on lists that are
sorted. This lab exercises algorithms for searching and sorting, and their
efficiency.
The (suggested) reading for this lab is Chapter 3 in the SG textbook.

- Take a look at the following binary search algorithm discussed in
class:
BinarySearch get n, a

Using this algorithm, search for target_{1}, a_{2}, ...a_{n}, t start = 1 end = n found = "false" repeat until found="true" or ... m = ceiling( (start + end)/2) //the middle between start and end if (t = a_{m}) then print "Target found at position " m found = "true" if (t < a_{m}) then end = m-1 if (t > a_{m}) then start = m+1 if (found="false") then print "Target not in the list"`t=3`in the list`2,4,5,7,9`. At some point the values of the variables`start`and`end`will be equal. What happens if we let the loop execute when`start=end`? What will the values of`start`and`end`be after this last interation? With this insight, how would you write the condition to stop the repeat loop (so that it executes one last time`start=end`)? - Assume you use the binary search algorithm to decide whether 35 is
in the following list:
3, 6, 7, 9, 12, 14, 18, 21, 22, 31, 43

What numbers are compared to 35? - Consider we perform binary search on the following list of names:
Arturo, Elsa, JoAnn John, Jose, Lee, Snyder, Tracy

- When searching to see if Elsa is in the list, what names will be compared to Elsa?
- When searching to see if Tracy is in the list, what names will be compared to Tracy?
- When searching to see if Emille is in the list, what names will be compared to Emille?

- Draw the tree data structure that describes binary search on the 8-element list in the previous problem. What is the number of comparisons in the worst case? Give an example of a name to search that requires that many comparisons.
- Draw the tree structure that describes binary search on a list with 16 elements. What is the number of comparisons in the worst case?
- Assume we are using binary search to search for a target that is
smaller than all elements in the list. What happens to the variable
`start`throughout the algorithm? What values does it take? What about the variable`end`? - 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 takes
order of
`n`in the worst case. If the list size is^{2}`n=100,000`, about how many worst-case searches must be done before the second before the second alternative is better in terms of number of comparisons? (*Hint: Let*)`p`represent the number of searches done. - Write an algorithm that evaluates a polynomial at a point. The
algorithm should read in from the user a list of
`n+1`coefficients,`a`and a value_{0}, a_{1}, a_{2},...,a_{n}`x`. The algorithm should compute and print the value`a`._{0}+ a_{1}x+a_{2}x^{2}...+a_{n}x^{n}As part of your algorithm you will need to compute the product

`x`(you cannot assume that you can use^{n}`x`in pseudocode).^{n}Analyze the worst-case and best-case running time of your algorithm (the order of magnitude, or theta-notation is enough).

- Recall the
`SelectionSort`algorithm discussed in class. The algorithm, at a high level of abstraction, is given bellow:SelectionSort get n, a

_{1},...a_{n}set unsortedEnd = n repeat until (unsortedEnd = 1) find the position of the largest element among a_{1}, a_{2},..,a_{unsortedEnd}assign this position to pl swap a_{pl}with a_{unsortedEnd}unsortedEnd = unsortedEnd - 1 print "The list in sorted order is" a_{1}, a_{2},..,a_{n}- Show how the algorithm works if the input list is
`3,1,2,5,4` - Describe in words what happens in the first iteration of the repeat loop (one sentence).
- Describe in words what happens in the second iteration of the repeat loop (one sentence).
- How many times is the repeat loop executed for a list of size
`n`? - For each of the 4 steps within the repeat loop, write down how many instructions they take in the first iteration of the loop.
- Same, for the second iteration of the loop.
- Same, for the last iteration of the loop.
- By adding up the numbers that you got above, find out the total running
time (number of instructions) of
`SelectionSort`.

- Show how the algorithm works if the input list is
- 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 in6 1 5 3 2 6

your algorithm should printThe 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).

- 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 list4 3 1 2 5 7 6

the median is 4. For the list5 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).

- The 11 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!
*