Recurrence Relations

A few handy definitions:

function
returns a single result via the function name
other uses are "side effects" to be avoided
subprogram
returns one or more results via parameters
parameters may be input, output or both
may use call-by-value, call-by-reference, call-by-name, etc
FLOOR
a function that rounds down
MERGE
a subprogram that merges two sorted sublists

Consider the following pseudocode:

subprogram A(array B(l:r))
   if (r-l) >= 1 then
   begin
      m <- FLOOR[(l+r)/2]
      call A(B(l:m))
      call A(B(m+1:r))
      call MERGE(B(l:m), B(m+1:r))
   endif
endsubprogram

What does A do?

MergeSort.

Example

{7 3 5 1} Original array
{7 3} {5 1} Split into two
{7} {3} {5} {1} Split each of above into two
{3 7} {1 5} Merge the first two and second two
{1 3 5 7} Merge the two into one

Analysis

Time

T(n) = 2T(n/2) + n
Solve by inspection. Try T(n) = n log(n).
n log(n) ?= 2 (n/2) log(n/2) + n = n (log(n/2) + 1)
                                 = n (log(n/2) + log(2))
                                 = n log((n/2)*2)
                                 = n log(n)
Thus T(n) really is n log(n).

Space

MERGE is easy to perform in linear extra space. Just use merge-forward or merge-backward with n/2 extra storage cells.

The situation gets slippery, however, when only constant extra space is available. See, for example, a relevant research paper.