02 Class
02 Class
ok!
j=3
j=4
constants
j=5
i j=6
A[6]
j=6
1 4 8 9 11 7
t6 = 4
X
1 tj j
1 4 7 8 9 11
1&2-1a
1&2-3 1&2-4
n 2.3 Designing Algorithms
T(n) = c1n+(c2+c4+c8)(n-1)+c5 t j +
j 2 Divide-and-Conquer : [b4]
1&2-4a
t j 1
n
(c6+c7) *not similar
j 2
Divide: (into the same problems of [bf5]
smaller size)
Best-case: Conquer : recursively solve each subproblems
[b6]
= (n ) [b2][b3]
q
(rate of growth, order of growth) 1&2-4c
1&2-5a
3 6
1 2 4 5
Figure 2.4 The operation of merge sort on the array A = 5, 2, 4, 7, 1, 3, 2, 6. The
lengths of the sorted sequences being merged increase as the algorithm progresses
from bottom to top[b8].
Analysis: (recurrence)
constant time
T(n) = (1) if n 1
2T (n / 2) (n ) if n 1
How to merge in Θ(n) time?
= (n log n)