Consider the equation
Here
is the number of times we have used the equation to
``reduce'' the value of
. Let's keep this up until we reach the
stopping condition, i.e., until
, or
. Then we get
That is,
, so T is the factorial function, and we have
``solved'' the ``recurrence equation'' above. The method we used is
called ``iteration'' and it is quite general and powerful.
Here is another example, with boundary condition
:
If we keep this up until
(to reach the boundary condition) then we get
In fact, this very method can be used to prove the ``Master Theorem'' which gives a formula for solutions to many (but not all) recurrences. Consult the Notes on this Theorem to learn how it works.
Using either the iteration method directly, or using the Master Theorem (in a special way), one can solve the Merge Sort runtime recurrence:
to get
Thus Merge Sort has a runtime much better than (the worst-case or average case) for Insertion Sort.