2 Running Time
2 Running Time
Best case occurs for already sorted array, in which case no values need to
moved from one place to another. Running time is Q(n).
Worst case occurs for reverse sorted array, in which case tj, the number of
executions of the loop test, is at its maximum. Running time is Q(n2).
Even if we assume that tj = j/2 on average, we still get running time Q(n2).
EXAMPLE:
MERGE-SORT( A, p, r )
if p < r
q = floor((p+r)/2)
MERGE-SORT( A, p, q )
MERGE-SORT( A, q+1, r )
MERGE( A, p, q, r )
For the function MERGE, please see [CLRS] <-- SELF STUDY.
Combining these:
Q(1), if n = 1
T(n) =
2T(n/2) + Q(n), if n > 1
A so-called “master theorem" sets out the general case, as we will see later.
OTHER EXAMPLES