## Exam 2

### 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 a divide-and-conquer algorithm to compute kn for k > 0 and integer n >= 0.

2. Answer these questions about your divide-and-conquer algorithm for Problem 1. Assume that n = 2m for some integer m.

• Give a recurrence relation for counting the number of multiplications done by your algorithm.

• Solve your recurrence relation.

• How does this number compare to the number of multiplications done by a simple decrease-and-conquer algorithm for this problem?

3. Here is an optimized implementation of insertion sort from Session 14:
```     INPUT: array A[0..n-1]

1  for i := 1 to n-1
2    v := A[i]
3    j := i - 1
4    while j ≥ 0 AND A[j] > v
5      A[j+1] := A[j]
6      j := j - 1
7    A[j+1] := v
```

Trace this algorithm for the input [89 45 68 90 29] and write down the following information just before executing Line 7 on each pass:

• the state of the array
• the number of value comparisons done on the just-completed pass through the while-loop

4. Consider the partition problem: Given n positive integers, partition them into two disjoint subsets such that, when we sum the elements in each subset, the two sums are equal.

• What makes this problem computationally hard to solve by brute force?

• Describe one of the combinatorial algorithms we studied in class that can help us solve this problem as efficiently as possible using brute force.

5. Suppose we are given an array A[0..n-1] of integers, in no particular order. Suppose further that we have an operator reverse(j) that can reverse the integers in slots 0 through j in a single step.

Design a decrease-and-conquer algorithm to sort the array. Use the following invariant:

After k steps, the largest k numbers are in their correct positions.

Eugene Wallingford ..... wallingf@cs.uni.edu ..... March 13, 2014