

Remember to:
T(n) = 2T(n/2 + 17) + nis O(n log n).
Note: In case you find the notation above ambiguous, T(n/2 + 17) = T((n/2) + 17).
if
otherwise
is reasonable (note that the limit of that series (i.e. a power series) approaches 2 as
tends to
).
is
. We can now write a general formula for the total of each level:
is for each level. Fortunately since
this will exactly correspond to the contribution of the nodes of each level to the overall tree. Hence we need to find
such that:
.
, the contribution of each node is at most
as defined by the base case of the recurrence. We can now define the total contribution of the final level as
. Thus we have the total value of the entire tree:
OPTIONAL PART (20 extra credit points): In addition to using the recursion-tree method above, use the (partial) substitution method to prove that T(n) is O(n log n). Remember that the substitution method involves 2 steps:
- (5 points) Guess a good upper bound. Explain why your guess makes sense.
- (15 points) Use mathematical induction to refine your guess (i.e., select the appropriate constants and/or add terms to your guess) and to show that the solution works.
into two pieces which add up to
itself. Because of this, the total contribution of each level in the "unrolled" recurrence will be
. This is demonstrated by examining the first few levels of the recurrence tree:
, if 
, otherwise
, however, changes at different rates depending on which of the recursive uses of
one considers. The one on the left in the definition of
will decrease much faster than the one on the right. Fortunately we are dealing here with upper bounds and thus let's consider the maximum depth of the recurrence tree and use this value with the assumption that over all the levels in between, the contribution of the nodes is
. This is certainly going to be an overestimate as really some parts of the recurrence tree stop short of this maximum level.
as the size of the input to
at level
for the most slowly decreasing branches of the tree and we can see the following:
. Remember that we are working to establish an upper bound and hence overestimating is perfectly fine.
levels each contributing
and the bottom level contributing an additional
to the final sum. We can thus write the expression for the sum total of the entire tree:
we could set our guess at
which would be fine but given the significant presence of fractions over three, let's use the log base 3 instead:
.
then we have
. Thus
is:
to be, we need to pick a
that would cancel out the right part of the above expression or more precisely:
. One last check is necessary to make sure that the base case of the recurrence agrees. If
we can see that
is actually negative and hence our solution for
will not be above
.
Input:
- An array A of n positions, containing a sorted (in ascending order) sequence of
numbers. That is A[1] ≤ A[2] ≤ ... ≤ A[n].
- A value v.
Output:
- An index i such that v = A[i], OR
the message "v is not in A" if v doesn't appear in A.
Consider the following divide-and-conquer solution to this problem.
This solution is called binary search: Compare the midpoint of the
array A with v and eliminate half of the array from further consideration,
until either v is found in A, or no elements in A remain under consideration.