## Exam 1

### Instructions

• The quiz consists of five questions. Be sure that you have all of these and that they are all legible.

• Read all questions and their instructions thoroughly before you begin. It is always worth your time to plan ahead!

• You have also been given two blank pages. Use these sheets for your quiz answers. Be sure to write your name on each sheet.

• The quiz is worth 60 points. Each question is worth twelve points.

• Points will be awarded based on your explicit answers. Partial credit will be given where possible, so show all of your work.

• Whenever you are asked to answer or describe "briefly", a one- or two- sentence response should be enough to express the solution.

• You may keep the quiz questions when you are done, for future reference.

• The quiz lasts forty-five (45) minutes. It is due at 1:45 PM.

### Problems

1. Design an algorithm to determine if two words are anagrams of one another, that is, whether the two words have exactly the same letters, only in a different order. For example, late is an anagram of teal. How efficient is your algorithm?

2. For each of the following terms, say whether the term is true of your algorithm for Problem 1 and explain briefly why or why not.
• top-down
• zoom-in
• stable
• greedy

3. We have a list of 2n numbers. We would like divide the list into two sub-lists of n numbers each, L1 and L2, such that sum(L1)-sum(L2) is as large as possible.

Design an algorithm to produce two such sub-lists. Is your algorithm top-down, bottom-up, or neither?

4. Consider this algorithm for sorting an array:
```         INPUT:  array initial[0..n-1] of values that can be ordered

for i ← 1 to n do
j ← i
while j > 0 and initial[j-1] > initial[j] do
tmp ← initial[j-1]
initial[j-1] ← initial[j]
initial[j] ← tmp
j ← j - 1
```

Write brief answers for each of the following:

• What is the basic operation of the algorithm?

• How many times is that operation executed for an array of size n? You may assume that the numbers in the array are distributed randomly.

• What is the efficiency class of this algorithm? Be sure to justify your answer.

5. Consider this recursive algorithm, foo(n):
```         if n = 0
return 0
else
return 3*foo(n-1) + 1
```

Write brief answers for each of the following:

• Define a recurrence relation for counting the number of recursive calls made by this algorithm.

• Solve your recurrence relation to determine how many calls are made for an input of size n.

Eugene Wallingford ..... wallingf@cs.uni.edu ..... February 10, 2014