Professional Documents
Culture Documents
Design and Analysis of Algorithms: Lecture 3 - 4
Design and Analysis of Algorithms: Lecture 3 - 4
of Algorithms
(CSE 373)
Lecture 3_4
Divide and Conquer (Mergesort)
Divide and Conquer
Recursive in structure
18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43
18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9
18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23
23 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14 23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14 23 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
98 23 45 14
23 98 14 45
14 23 45 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14
23 98 14 45
14 23 45 98
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67
23 98 14 45
14 23 45 98
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67
23 98 14 45
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67
23 98 14 45 6
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67
23 98 14 45 6 67
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67
14 23 45 98
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42 45
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67
Merge
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42
23 98 14 45 6 67 33 42
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
Merge
98 23 45 14 6 67 33 42
6 14 23 33 42 45 67 98
Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers
6 18 56 62 1 9 15 43
Merging two sorted subsequeces
Unsorted
6 18 56 62 1 9 15 43
Sorted Sorted
Merging two sorted subsequeces
6 18 56 62 1 9 15 43
Merging
Merging two sorted subsequeces
6 18 56 62 1 9 15 43
Merging
Left half
Right half
1
Left half
Right half
1
Left half
Right half
1 6
Left half
Right half
1 6
Left half
Right half
1 6 9
Left half
Right half
1 6 9
Left half
Right half
1 6 9 15
Left half
Right half
1 6 9 15
Left half
Right half
1 6 9 15 18
Left half
Right half
1 6 9 15 18
Left half
Right half
1 6 9 15 18 43
Left half
Right half
1 6 9 15 18 43
Left half
Right half
1 6 9 15 18 43 56 62
Left half
Right half
1 6 9 15 18 43 56 62
Left half
Right half
L 6 8 26 32 R 1 9 42 43
i i i i i j j j j j
Comp 122
Merging two sorted subsequeces
Merge(A, p, q, r)
1 n1 q – p + 1
2 n2 r – q Input: Array containing
3 for i 1 to n1 sorted subarrays A[p..q] and
4 do L[i] A[p + i – 1] A[q+1..r].
5 for j 1 to n2 Output: Merged sorted
6 do R[j] A[q + j] subarray in A[p..r].
7 L[n1+1]
8 R[n2+1]
9 i1
10 j1
11 for k p to r Sentinels, to avoid having to
12 do if L[i] R[j] check if either subarray is
13 then A[k] L[i]
fully copied at each step.
14 ii+1
15 else A[k] R[j]
16 jj+1
Time complexity of Merge
Merge(A, p, q, r) //Let r-p+1 = n
1 n1 q – p + 1 //O(1)
2 n2 r – q //O(1)
Input: Array containing
3 for i 1 to n1 //O(q-p+1)
sorted subarrays A[p..q] and
4 do L[i] A[p + i – 1] A[q+1..r].
5 for j 1 to n2 //O(r-q)
6 do R[j] A[q + j]
Output: Merged sorted
subarray in A[p..r].
7 L[n1+1]
8 R[n2+1]
9 i1
10 j1
11 for k p to r //O(r-p+1) = O(n)
12 do if L[i] R[j]
13 then A[k] L[i]
14 ii+1
15 else A[k] R[j]
16 jj+1
//Total time: O(n)
Merge Sort
MergeSort (A, p, r) // sort A[p..r] by divide & conquer
1 if p < r
2 then q (p+r)/2 //divide
3 MergeSort (A, p, q) //conquer
4 MergeSort (A, q+1, r) //conquer
5 Merge (A, p, q, r) //combine: merge A[p..q] with A[q+1..r]
For the original problem, Each of the size n/2 problems has
we have a cost of cn, plus a cost of cn/2 plus two
two subproblems each of subproblems, each costing T(n/4).
size (n/2) and running time
T(n/2).
cn
cn
Cost of divide
and merge.
cn/2 cn/2
T(n/2) T(n/2)
T(n/4) T(n/4) T(n/4) T(n/4)
Cost of sorting
subproblems.
Recursion Tree for Merge Sort
Continue expanding until the problem size reduces to 1.
cn cn
cn/2 cn/2 cn
log2n + 1
c c c c c c cn
Total : cnlog2n + cn
Recursion Tree for Merge Sort
Continue expanding until the problem size reduces to 1.
cn •Each level has total cost cn.