In class we saw the “Master Theorem” for solving recurrences:

Master Theorem. Suppose the running time $$T(n)$$ of a (recursively defined) method satisfies $$T(n) = a T(n / b) + f(n)$$. Define $$c = \log_b a$$. Then:

1. If $$f(n) = O(n^d)$$ for $$d < c$$ then $$T(n) = O(n^c)$$
2. If $$f(n) = \Theta(n^c \log^k n)$$ then $$T(n) = O(n^c \log^{k+1} n)$$
3. If $$f(n) = \Omega(n^d)$$ for $$d > c$$, then $$T(n) = O(f(n))$$

For the following methods, apply the Master Theorem to derive a bound on the running time of the method. In particular, for each method you should compute the values $$a, b, c,$$ and the function $$f$$ to determine which (if any) of the three conditions above apply. In both cases, the size $$n$$ of the input is $$n = j - i$$.

1. Binary Search.

1
2
3
4
5
6
7
8
9
BinarySearch(a, val, i, j):
if j = i then return false
if j - i = 1 then return a[i] = val
m <- (j + i) / 2
if a[m] > val then
return BinarySearch(a, val, i, m)
else
return BinarySearch(a, val, m, j)
endif

2. Merge Sort. Assume that the Merge procedure runs in time $$O(n)$$.

1
2
3
4
5
6
7
8
MergeSort(a, i, j):
if j - i = 1 then
return
endif
m <- (i + j) / 2
MergeSort(a,i,m)
MergeSort(a,m,j)
Merge(a,i,m,j)


### Solution

1. For the BinarySearch method, a call of size $$n = j - i$$ makes a single recursive call to BinarySearch at either line 6 or line 8. The size of the recursive call is $$n/2$$. All other operations take time $$O(1)$$, using the notation of the Master Theorem, we have

\begin{align*} a &= 1\\ b &= 2\\ c &= \log_2 1 = 0\\ f(n) &= O(1) = O(n^0) = O(n^c). \end{align*}

Therefore, we fall into case 2 of the theorem, so the running time is $$T(n) = O(n^0 \log n) = O(\log n)$$.

2. For MergeSort, a call of size $$n = j - i$$ makes two recursive calls to MergeSort, each of size $$n/2$$ (in lies 6 and 7). All operations take time $$O(1)$$, except the call to Merge, which is assumed to take time $$O(n)$$. Thus, in the notation of the Master Theorem we have

\begin{align*} a &= 2\\ b &= 2\\ c &= \log_2 2 = 1\\ f(n) &= O(n) = O(n^c). \end{align*}

These parameters again put us in case 2 of the Master Theorem. Applying this case we get $$T(n) = O(n^c \log n) = O(n \log n)$$.