Note: You are looking at a static copy of the former PineWiki site, used for class notes by James Aspnes from 2003 to 2012. Many mathematical formulas are broken, and there are likely to be other bugs as well. These will most likely not be fixed. You may be able to find more up-to-date versions of some of these notes at http://www.cs.yale.edu/homes/aspnes/#classes.

# 1. Extracting recurrences from recursive algorithms

```{ Invariant: A[lo] < target < A[hi] }
BinarySearch(A, lo, hi, target):
if hi <= lo + 1:
return hi
mid = floor((lo + hi) / 2)
if A[mid] = target: return mid
if A[mid] < target: return BinarySearch(A, mid, hi, target)
if A[mid] > target: return BinarySearch(A, lo, mid, target)```

To analyze running time: use (hi - lo) as parameter n. Then

• T(1) = Theta(1)
• T(n) <= T(n/2) + Theta(1) [more or less]

```MergeSort(A):
if length(A) > 1:
MergeSort(A[1..n/2])
MergeSort(A[n/2+1..n])
Merge(A[1..n/2], A[n/2+1..n], A)  {we assume this is Theta(n)}```

Cost is now:

• T(1) = Theta(1)
• T(n) = 2 Theta(n/2) + Theta(n)

2014-06-17 11:58