Professional Documents
Culture Documents
03 DivideConquer Merge Sort
03 DivideConquer Merge Sort
03 DivideConquer Merge Sort
Merge Sort
them recursively
Combine the solutions to the
Merge sort
Divide step is trivial – just split the list into two equal parts
Work is carried out in the conquer step by merging two
sorted lists
Partition
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6
7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6
Merge Sort 7
Execution Example
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6
Merge Sort 8
Execution Example
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
Merge Sort 9
Execution Example
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
Merge Sort 10
Execution Example
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
Merge Sort 11
Execution Example
Merge
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
Merge Sort 12
Execution Example
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
Merge Sort 13
Execution Example
Merge
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 8 6
722 7 9 4 4 9 3 8 3 8 6 1 1 6
Merge Sort 14
Execution Example
7 29 4 2 4 7 9 3 8 6 1 1 3 6 8
722 7 9 4 4 9 3 8 3 8 6 1 1 6
Merge Sort 15
Execution Example
Merge
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9 3 8 6 1 1 3 6 8
722 7 9 4 4 9 3 8 3 8 6 1 1 6
Merge Sort 16
Merge Sort: Running Time
T(n) = (1) if n = 1
2T(n/2) + (n) if n > 1
equivalently
T(n) = b if n = 1
2T(n/2) + bn if n > 1
Draw the recursion tree for the recurrence relation and look for a
pattern:
time
depth T’s size
0 1 n bn
1 2 n/2 bn
i 2i n/2i bn
… … … …
(b)
(c)
Cormen: Figure 2.5 How to construct a recursion tree for the recurrence
. Part (a) shows , which progressively expands in (b)–(d) to form the
recursion tree. The fully expanded tree in part (d) has levels (i.e., it has
height , as indicated), and each level contributes a total cost of . The total
cost, therefore, is , which is,