The standard Divide-and-Conquer algorithm usually works as follows:
- spend some time handling the base case
- if not at the base case, divide the problem up into smaller subproblems: D(n)
- make recursive calls to handle all the subproblems created on step 2: T(n/b)
- combine results created by recursive calls on step 3: C(n)
利用遞迴分析:
T(n) = a * T(n/b) + f(n)
- a = number of subproblems
- n/b = size of the subproblems
- f(n) = D(n) + C(n)
以Max-Heapify為例來分析:
- a = 1, i.e., there is only one recursive call made
- n/b = (2/3) * n, is the worst case size of the one subproblem. The size of the left subtree is (2/3) * n. 也就是說 when the left subtree has one entire full level as compared to the right subtree. The worst case is when the last level of the tree is half full.
- f(n) = Q(1), all the work done in lines 1 - 9 above takes a constant time
- T(n) = T(2n/3) + Q(1)
Max-Heapify演算法如下:
Max-Heapify(A,i)
1 l ← Left(i)
2 r ← Right(i)
3 if l ≤ heap-size[A] and A[l] > A[i]
4 then largest ← l
5 else largest ← i
6 if r ≤ heap-size[A] and A[r] > A[largest]
7 then largest ← r
8 if largest ≠ i
9 then exchange A[i] ↔ A[largest]
10 Max-Heapify(A, largest)




