## Homework Assignment 1

### Introduction to Algorithms and Analysis

#### Tasks

1. Given two sorted lists of values, we would like to find the list of common elements, also sorted. For example, given [2, 2, 3, 7] and [2, 3, 3, 3, 5, 7], the list of common elements is [2, 3, 7].

In Session 4, we saw one algorithm for solving this problem.

• Design either a new top-down algorithm or a new bottom-up algorithm to solve this problem.

• Explain why your algorithm is top-down or bottom-up.

• Let m and n be the lengths of the input lists. State the maximum number of comparisons that your algorithm makes in terms of m and n.

2. Consider the Comparison Counting Sort algorithm below, which appears in Levitin's text. Given an unsorted array of values, it produces a new, sorted array. It does so by counting for each item v the number of items in the array that are less than v, and using that information to put v in its appropriate spot.

• Demonstrate how this algorithm operates on this input array: [93, 13, 37, 14].

• Explain why this algorithm is (or isn't) stable.

• Explain why this algorithm is (or isn't) in-place.

3. Read the first two paragraphs of the section labeled What is a patent? on US Patent and Trademark Office's patents web page.

Should the inventor of an algorithm be eligible to receive a utility patent? Why, or why not? (Limit yourself to three to five sentences.)

#### Deliverables

By the due date and time, submit a hardcopy of your solutions to my mailbox in the department office, or to the front desk in our classroom before class begins.

#### Comparison Counting Sort

```    // Input : Array A[0..n-1] of orderable values
// Output: Array S[0..n-1] of A's elements,
//         sorted in nondecreasing order

for i ← 0 to n-1 do
Count[i] ← 0

for i ← 0 to n-2 do
for j ← i+1 to n-1 do
if A[i] < A[j]
then Count[j] ← Count[j] + 1
else Count[i] ← Count[i] + 1

for i ← 0 to n-1 do
S[Count[i]] ← A[i]

return S
```

(This algorithm appears on Page 34 of The Design and Analysis of Algorithms by Anany Levitin. I have made a few trivial typographical changes to the presentation.)

Eugene Wallingford ..... wallingf@cs.uni.edu ..... January 23, 2014