Lecture-6_Sorting Algorithms_MergeSort
Lecture-6_Sorting Algorithms_MergeSort
1
Today’s Contents
• Divide and Conquer Approach
• Merge Sort
• Complexity Analysis
• Questions for You!
2
Divide & Conquer Approach
3
Divide & Conquer Approach
4
Merge Sort
5
Example
6
p = first Index
r = last index
1 2 3 4 5 6 7 8
A[n] 5 2 4 7 3 1 2 6 q ← ⎣(p + r) / 2⎦
7
p = first Index
r = last index
1 2 3 4 5 6 7 8
A[n] 5 2 4 7 3 1 2 6 q ← ⎣(p + r) / 2⎦
q = (1 + 8 ) / 2 = 4
8
p = first Index
1 2 3 4 5 6 7 8
r = last index
Divid 5 2 4 7 3 1 2 6 q ← ⎣(p + r) / 2⎦
e q = (1 + 8 ) / 2 = 4
1 2 3 4 5 6 7 8
5 2 4 7 3 1 2 6
9
q ← ⎣(p + r) / 2⎦
1 2 3 4 5 6 7 8
Divid 5 2 4 7 3 1 2 6
e
1 2 3 4 5 6 7 8
q = (5 + 8 ) /2
q = (1 + 4 ) /2
5 2 4 7 3 1 2 6 =6
=2
10
q ← ⎣(p + r) / 2⎦
1 2 3 4 5 6 7 8
Divid 5 2 4 7 3 1 2 6
e
1 2 3 4 5 6 7 8
q = (5 + 8 ) /2
q = (1 + 4 ) /2
5 2 4 7 3 1 2 6 =6
=2
11
q ← ⎣(p + r) / 2⎦
1 2 3 4 5 6 7 8
Divid 5 2 4 7 3 1 2 6
e
1 2 3 4 5 6 7 8
q = (5 + 8 ) /2
q = (1 + 4 ) /2
5 2 4 7 3 1 2 6 =6
=2
1 2 3 4 5 6 7 8
5 2 4 7 3 1 2 6
12
q ← ⎣(p + r) / 2⎦
1 2 3 4 5 6 7 8
Divid 5 2 4 7 3 1 2 6
e
1 2 3 4 5 6 7 8
5 2 4 7 3 1 2 6
1 2 3 4 5 6 7 8
5 2 4 7 3 1 2 6
13
1 2 3 4 5 6 7 8
Divid 5 2 4 7 3 1 2 6
e
1 2 3 4 5 6 7 8
5 2 4 7 3 1 2 6
1 2 3 4 5 6 7 8
5 2 4 7 3 1 2 6
1 2 3 4 5 6 7 8
5 2 4 7 3 1 2 6
14
Conque 1 2 3 4 5 6 7 8
1 2 2 3 4 5 6 7
r
and
Merge
1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
1 2 3 4 5 6 7 8
2 5 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 3 1 2 6
15
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
Conque
r 1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
and
Merge
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
16
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
Conque
r 1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
and
Merge
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
2 5 4 7 1 3 2 6
17
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
Conque
r 1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
and
Merge
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
5 2 4 7 1 3 2 6
1 2 3 4 5 6 7 8
2 5 4 7 1 3 2 6
1 2 3 4 5 6 7 8
2 4 5 7 1 2 3 6
18
Conque
r
and
Merge
19
i j
20
i i i i i j j j j j
A 1 2 2 3 4 5 6 7
21
22
Algorithm of Merge Sort
MERGE-SORT(A, p, r) 1 2 3 4 5 6 7 8
1. if ( p < r ) 5 2 4 7 1 3 2 6
⎣
2. q ← (p + r) / 2 ⎦
3. MERGE-SORT(A, p, q)
4. MERGE-SORT(A, q + 1, r)
5. MERGE(A, p, q, r)
23
MERGE(A, p, q, r) 1 2 3 4 5 6 7 8
1. n1 = q – p + 1 5 2 4 7 1 3 2 6
2. n2 = r - q
3. Let L[1 . . n1 + 1] and R[1 . . n2 + 1] be two new arrays
4. for i = 1 to n1
5. L[ i ] ← A[p + i - 1]
6. for j = 1 to n2
7. R[ j ] ← A[ q + j ]
8. L[n1 + 1] ← ∞
9. R[n2 + 1] ← ∞ j
i
10. i ← 1
11. j ← 1
12. for k ← p to r A
13. if L[ i ] ≤ R[ j ]
14. then A[k] ← L[ i ]
15. i←i+1
16. else A[k] ← R[ j ]
17. j←j+1 24
Example
25
MERGE(A, p, q, r)
Algorithm of n1 = q – p + 1
Merge Sort n2 = r - q
Let L[1 . . n1 + 1] and R[1 . . n2 + 1] be two new
arrays
MERGE-SORT(A, p, r) for i = 1 to n1
1. if p < r L[ i ] ← A[p + i - 1]
2. q ← ⎣ (p + r ) / 2 ⎦ for j = 1 to n2
3. MERGE-SORT(A, p, q)
R[ j ] ← A[ q + j ]
4. MERGE-SORT(A, q + 1, r)
L[n1 + 1] ← ∞
5. MERGE(A, p, q, r)
R[n2 + 1] ← ∞
i←1
j←1
for k ← p to r
if L[ i ] ≤ R[ j ]
then A[k] ← L[ i ]
i←i+1
else A[k] ← R[ j ]
j←j+1
26
Time Complexity
MERGE-SORT(A, p, r)
1. if p < r
2. q← ⎣ (p + r ) / 2 ⎦
3. MERGE-SORT(A, p, q)
T(n/2)
4. MERGE-SORT(A, q + 1, r) T(n/2)
5. MERGE(A, p, q, r) O(n)
Complexity:
28
Advantage and Drawbacks
▪ Advantages
- Guaranteed to run in O(nlogn)
▪ Disadvantage
- Requires extra space ≈ O(n)
29
Question for you
1. Which Algorithm will you choose?
- For Sorted Data List
- For Unsorted Data List
3. Sort the given data set using both the Merge Sort. Show
each steps.
26 13 6 8 20 22 13
30
Some References
https://visualgo.net/en/sorting
31
Thank you!
32