0% found this document useful (0 votes)
11 views33 pages

Merge Sort

Merge sort

Uploaded by

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

Merge Sort

Merge sort

Uploaded by

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

B.

TECH V SEM CSE


ACADEMIC YEAR: 2021-2022
Course Name: Design and Analysis of Algorithm
Topic: : Divide and Conquer: Merge Sort and Analysis

Course code : CS 3102


Credits : 4
Mode of delivery : Hybrid (Power point presentation)
Faculty : Dr. Ajit Noonia
Email-id : [email protected]

1
Divide and Conquer
Divide and Conquer
Divide-and-Conquer

1 2 3 4 5 6 7 8

7 2 9 4 3 8 6 1
⚫ To sort an array of n elements, we perform the following steps in
sequence:
⚫ If n < 2 then the array is already sorted.
⚫ Otherwise, n > 1, and we perform the following three steps in
sequence:
1. Sort the left half of the array using Merge Sort.
2. Sort the right half of the array using Merge Sort.
3. Merge the sorted left and right halves.

5
Merge Sort algorithm
l m h MergeSort(list, low, high)
0 1 2 3 4 5 6 7 {
7 2 9 4 3 8 6 1 if (low == high) return;

int middle = (low + high) / 2;


MergeSort(list, low, middle);
0-7
MergeSort(list, middle + 1, high);
Merge(list, low, middle, high);
}
0-3 4-7

0-1 2-3 4-5 6-7

0-0 1-1 2-2 3-3 4-4 5-5 6-6 7-7


Merging
l m h
Idea for merging: 0 1 2 3 4 5 6 7

Two piles of sorted cards 2 4 5 7 1 2 3 6


Choose the smaller of the two top cards
Remove it and place it in the output pile
Repeat the process until one pile is empty
Take the remaining input pile and place it face-down onto the output pile

A1 A[p, q]
A[p, r]

A2 A[q+1, r]

7
Merge-Sort: Merge
Sorted

A:

merge
Sorted Sorted
FirstPart SecondPart

A:

A[left] 8
A[middle] A[right]
Merge-Sort: Merge Example

A: 2
5 3
5 7 28
15 8 30
1 4
6 5 14
10 6

L: R:
3 5 15 28 6 10 14 22

Temporary Arrays
Merge-Sort: Merge Example

A:
3
1 5 15 28 30 6 10 14

k=0

L: R:
3
2 15
3 28
7 30
8 6
1 10
4 14
5 22
6

i=0 j=0
Merge-Sort: Merge Example

A:
1 2
5 15 28 30 6 10 14

k=1

L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6

i=0 j=1
Merge-Sort: Merge Example

A:
1 2 3 28 30
15 6 10 14

k=2

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=1 j=1
Merge-Sort: Merge Example

A:
1 2 3 4 6 10 14

k=3

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=1
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 10 14

k=4

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=2
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 10 14

k=5

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=3
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 7 14

k=6

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=4
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 7 8
14

k=7

L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6

i=3 j=4
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 7 8

k=8

L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6

i=4 j=4
Merge Algorithm(list, low, middle, high)

Merge Approach
{
int i, j, k; int result[ ];
i = low; j = middle+1; k = 0;
l m h while ((i <=middle) && (j <= high)) {
if(list[i] <= list[j]) {
0 1 2 3 4 5 6 7
result[k] = list[i]; k++; i++;
A 2 4 5 7 1 2 3 6 }
else {
result[k] = list[j]; k++; j++;
}
}
0 1 2 3 4 5 6 7 while(j<=h){
R result[k] = list[j]; k++; j++;
}
while(i<=m){
result[k] = list[i]; k++; i++;
}
for(k=0;k<=h;k++) {
list[k] = result[k]
}
}
Execution Example

7 2 9 43 8 6 1

20
Execution Example

⚫ Recursive call, partition


7 2 9 43 8 6 1

7 29 4

21
Execution Example

⚫ Recursive call, partition


7 2 9 43 8 6 1

7 29 4

72

22
Execution Example

⚫ Recursive call, base case


7 2 9 43 8 6 1

7 29 4

72

7→7

23
Execution Example

⚫ Recursive call, base case


7 2 9 43 8 6 1

7 29 4

72

7→7 2→2

24
Execution Example

⚫ Merge
7 2 9 43 8 6 1

7 29 4

72→2 7

7→7 2→2

25
Execution Example

⚫ Recursive call, …, base case, merge


7 2 9 43 8 6 1

7 29 4

72→2 7 9 4 → 4 9

7→7 2→2 9→9 4→4

26
Execution Example

⚫ Merge
7 2 9 43 8 6 1

7 29 4→ 2 4 7 9

72→2 7 9 4 → 4 9

7→7 2→2 9→9 4→4

27
Execution Example

⚫ Recursive call, …, merge, merge


7 2 9 43 8 6 1

7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 6 8

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

28
Execution Example

⚫ Merge
7 2 9 43 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 6 8

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

29
Merge Sort – Time Complexity
MergeSort(list, low, high)
{
if (low == high) return;

int middle = (low + high) / 2;


MergeSort(list, low, middle);
MergeSort(list, middle + 1, high);
Merge(list, low, middle, high);
}
Merge Sort – Time Complexity
MergeSort(list, low, high)
{
if (low == high) return;

int middle = (low + high) / 2;


MergeSort(list, low, middle);
MergeSort(list, middle + 1, high);
Merge(list, low, middle, high);
}
Merge Sort – Time Complexity
MergeSort(list, low, high)
{
if (low == high) return;

int middle = (low + high) / 2;


MergeSort(list, low, middle);
MergeSort(list, middle + 1, high);
Merge(list, low, middle, high);
}
Merge Sort – Time Complexity

You might also like