0% found this document useful (0 votes)
9 views32 pages

Lecture-6_Sorting Algorithms_MergeSort

The document discusses the Divide and Conquer approach, specifically focusing on the Merge Sort algorithm, which involves dividing an array into subarrays, recursively sorting them, and then merging them back together. It provides a detailed algorithm for Merge Sort, including its time complexity of O(n log n) for both best and worst cases, along with advantages and disadvantages. Additionally, it poses questions for further consideration regarding the choice of sorting algorithms for different data scenarios.

Uploaded by

ovikhan753
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views32 pages

Lecture-6_Sorting Algorithms_MergeSort

The document discusses the Divide and Conquer approach, specifically focusing on the Merge Sort algorithm, which involves dividing an array into subarrays, recursively sorting them, and then merging them back together. It provides a detailed algorithm for Merge Sort, including its time complexity of O(n log n) for both best and worst cases, along with advantages and disadvantages. Additionally, it poses questions for further consideration regarding the choice of sorting algorithms for different data scenarios.

Uploaded by

ovikhan753
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

CSE214: Algorithms

Divide and Conquer Approach


and
Merge Sort

1
Today’s Contents
• Divide and Conquer Approach
• Merge Sort
• Complexity Analysis
• Questions for You!

2
Divide & Conquer Approach

The divide-and-conquer paradigm involves three steps at


each level of the recursion:
1. Divide the problem into a number of subproblems.
2. Conquer the subproblems by solving them
recursively. If the subproblem sizes are small
enough, however, just solve the subproblems in a
straightforward manner.
3. Combine the solutions to the subproblems into the
solution for the original problem.

3
Divide & Conquer Approach

4
Merge Sort

• The merge sort algorithm closely follows the divide-and-


conquer paradigm.
• Intuitively, it operates as follows.
1. Divide: Divide the n-element sequence to be sorted into
two subsequences of n/2 elements each.
2. Conquer: Sort the two subsequences recursively using
merge sort.
3. Combine: Merge the two sorted subsequences to
produce the sorted answer.

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⎦

Left Array, L = A(1 , q) Right Array, R = A[q + 1 , r]

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

Left Array, L = A(1 , q) Right Array, R = A[q + 1 , r]

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:

T(n) = [T(n/2) + T(n/2)] * O(n)


= 2T(n/2) * O(n)
= O(logn) * O(n)
= O(nlogn) 27
Complexity Analysis of Three Algorithms

Algorithm Best case Worst case

Selection Sort O(n2) O(n2)

Insertion Sort O(n) O(n2)

Merge Sort O(nlogn) O(nlogn)

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

2. Is Merge sort a In-Place Algorithm?

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

You might also like