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
| {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 | |||
T(n) = 2T(n/2) + 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).
The situation gets slippery, however, when only constant extra space is available. See, for example, a relevant research paper.