Lecture 7
Lecture 7
CS3610
05 Comparison
What?
Divide the problem into small distinct sub-problem(s)
Conquer the sub-problems by solving them recursively in same manner
1. Divide and conquer
1) Divide
2) Conquer . . .
. . . . . . 3) Combine
. . .
. . .
. . .
. . . . . . . . .
Why?
Often leads to efficient solutions.
1. Divide and conquer
3) Combine …etc
….
….
T(N) = Time of Normal Code + T(Recursive Code) + …
(Divide & Combine) etc (Conquer)
F2023 Analysis, Design of Algorithms 5
Divide and conquer
Typical case
Problem of size n
Subdivided into b instances, b>1
1. Divide and conquer
05 Comparison
Combine Trivial
• Analysis: T(N) = O(Divide & Combine) + T(Conquer)
1. T(N) = 1 T(N/2) + Θ(1)
2. Master Case#2: Θ(log(N))
vs
05 Comparison
7 2 9 4 3 8 6 1 → 1 3 8 6
9 4 → 4 3 8 → 3
7 2 6 1 → 1 6
9 8
7→ 2→ 9→ 4→ 3→ 8→ 6→ 1→
7 2 9 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 12
Merge sort
7 2⏐9 4 3 8 6 1 → 1 3 8 6
7 2 → 2 9 4 → 4 3 8 → 3
6 1 → 1 6
7 9 8
7→ 2→ 9→ 4→ 3→ 8→ 6→ 1→
7 2 9 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 13
Merge sort
7 2⏐9 4 3 8 6 1 → 1 3 8 6
9 4 → 4 3 8 → 3
7⏐2 6 1 → 1 6
9 8
7→ 2→ 9→ 4→ 3→ 8→ 6→ 1→
7 2 9 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 14
Merge sort
7 2⏐9 4 3 8 6 1 → 1 3 8 6
9 4 → 4 3 8 → 3
7⏐2 6 1 → 1 6
9 8
7→ 2→ 9→ 4→ 3→ 8→ 6→ 1→
7 2 9 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 15
Merge sort
7 2⏐9 4 3 8 6 1 → 1 3 8 6
9 4 → 4 3 8 → 3
7⏐2 6 1 → 1 6
9 8
7→ 9→ 4→ 3→ 8→ 6→ 1→
2→2
7 9 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 16
Merge sort
7 2⏐9 4 3 8 6 1 → 1 3 8 6
9 4 → 4 3 8 → 3
7⏐2→2 7 6 1 → 1 6
9 8
7→ 9→ 4→ 3→ 8→ 6→ 1→
2→2
7 9 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 17
Merge sort
7 2⏐9 4 3 8 6 1 → 1 3 8 6
3 8 → 3
7⏐2→2 7 9 4 → 4 9 6 1 → 1 6
8
7→ 4→ 3→ 8→ 6→ 1→
2→2 9→9
7 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 18
Merge sort
7 2⏐9 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6
3 8 → 3
7⏐2→2 7 9 4 → 4 9 6 1 → 1 6
8
7→ 4→ 3→ 8→ 6→ 1→
2→2 9→9
7 4 3 . 8 6 1
.
.
F2023 Analysis, Design of Algorithms 19
Merge sort
7 2⏐9 4→ 2 4 7 9 3 8 6 1 → 1 3 6 8
3 8 → 3
7⏐2→2 7 9 4 → 4 9 6 1 → 1 6
8
7→ 4→ 3→
2→2 9→9 8→8 6→6 1→1
7 4 3 .
.
.
F2023 Analysis, Design of Algorithms 20
Merge sort
7 2⏐9 4→ 2 4 7 9 3 8 6 1 → 1 3 6 8
3 8 → 3
7⏐2→2 7 9 4 → 4 9 6 1 → 1 6
8
7→ 4→ 3→
2→2 9→9 8→8 6→6 1→1
7 4 3 .
.
.
F2023 Analysis, Design of Algorithms 21
Merge sort
.
.
.
F2023 Analysis, Design of Algorithms 22
Merge sort
Merge example
3. Merge sort
14 23 45 98 6 33 42 67
.
.
.
F2023 Analysis, Design of Algorithms 23
Merge sort
Merge example
3. Merge sort
14 23 45 98 6 33 42 67
Merge .
.
.
F2023 Analysis, Design of Algorithms 24
Merge sort
Merge example
3. Merge sort
14 23 45 98 6 33 42 67
Merge .
.
.
F2023 Analysis, Design of Algorithms 25
Merge sort
Merge example
3. Merge sort
14 23 45 98 6 33 42 67
6 14
Merge .
.
.
F2023 Analysis, Design of Algorithms 26
Merge sort
Merge example
3. Merge sort
14 23 45 98 6 33 42 67
6 14 23
Merge .
.
.
F2023 Analysis, Design of Algorithms 27
Merge sort
Merge example
3. Merge sort
14 23 45 98 6 33 42 67
6 14 23 33
Merge .
.
.
F2023 Analysis, Design of Algorithms 28
Merge sort
Merge example
3. Merge sort
14 23 45 98 6 33 42 67
6 14 23 33 42
Merge
F2023 Analysis, Design of Algorithms 29
Merge sort
Merge example
3. Merge sort
14 23 45 98 6 33 42 67
6 14 23 33 42 45
Merge
F2023 Analysis, Design of Algorithms 30
Merge sort
Merge example
3. Merge sort
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67
Merge
F2023 Analysis, Design of Algorithms 31
Merge sort
Merge example
3. Merge sort
14 23 45 98 6 33 42 67
6 14 23 33 42 45 67 98
Merge
F2023 Analysis, Design of Algorithms 32
Merge sort
Θ(1) if n = 1
T (n) =
2T (n/2)+ Θ(n) if n > 1
Pseudocode s m e
Alg.: MERGE-SORT(A, s, e) 1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
if s < e
Divide
MERGE-SORT(A, s, m) Where’s
Conquer each step
MERGE-SORT(A, m + 1, e) ???
MERGE(A, s, m, e) Combine
s m e
Alg.: MERGE(A, s, m, e) 1 2 3 4 5 6 7 8
1. Compute n1 and n2 2 4 5 7 1 2 3 6
4. L[n1 + 1] ← ∞; R[n2 + 1] ← ∞ s m
5. i ← 1; j ← 1 L 2 4 5 7 ∞
6. for k ← s to e m+1 e
7. do if L[ i ] ≤ R[ j ] R 1 2 3 6 ∞
8. then A[k] ← L[ i ]
9. i ←i + 1
10. else A[k] ← R[ j ]
11. j←j+1
F2023 Analysis, Design of Algorithms 35
Merge sort
Discussion
Running time insensitive of the input
Advantage:
Guaranteed to run in Θ(nlgn)
3. Merge sort
05 Comparison
10
40 20 10 80 60 50 7 30
0
pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
pivot_index = 0
10
40 20 10 80 60 50 7 30
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
pivot_index = 0
10
40 20 10 30 60 50 7 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
pivot_index = 0
10
40 20 10 30 60 50 7 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
pivot_index = 0
10
40 20 10 30 60 50 7 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
pivot_index = 0
10
40 20 10 30 60 50 7 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
pivot_index = 0
10
40 20 10 30 60 50 7 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
5. Swap data[j] and data[pivot_index]
pivot_index = 0
10
40 20 10 30 7 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
4. While j > i, go to 1
5. Swap data[j] and data[pivot_index]
10
pivot_index = 4 7 20 10 30 40 50 60 80
0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Partition result
10
7 20 10 30 40 50 60 80
0
4. Quick sort
10
7 20 10 30 40 50 60 80
0
4. Quick sort
Partition n - 1
4. Quick sort
05 Comparison
Selection sort:
Easy idea (Bruteforce)
Min number of swaps (O(N) swaps)
O(n2) in ALL cases
5. Comparison
Bubble sort:
Easy idea (Bruteforce)
O(n) sorted case
O(n2) in other cases
Insertion sort:
Easy to code
Fast on small inputs (less than ~50 elements)
Fast on nearly-sorted inputs
5. Comparison
Merge sort:
Divide-and-conquer:
Split array in half
Recursively sort subarrays
5. Comparison
Quick sort:
Divide-and-conquer:
Recursive sort
Partition array into two subarrays (1st subarray < 2nd subarray)
5. Comparison
Quick sort:
Can be enhanced by
Randomly shuffle the input array
Randomly select the pivot
5. Comparison