SlideShare a Scribd company logo
Merge sort Analysis and complexity
Design and Analysis of Algorithms
1
Divide and Conquer Approach
Step 1:
• If the problem size is small, solve this problem
directly
• Otherwise, split the original problem into 2 or
more sub-problems with almost equal sizes.
Step 2:
• Recursively solve these sub-problems by applying
this algorithm.
Step 3:
• Merge the solutions of the sub- problems into a
solution of the original problem.
2
• Time complexity:
– where S(n) is time for splitting
– M(n) is time for merging
– b and c are constants
Example
• Binary search
• Quick sort
• Merge sort
T(n)=



2T(n/2) + S(n) + M(n)
b
, n  c
, n < c
Advanced Algorithms Analysis and Design
Time Complexity of General Algorithms
Merge-Sort
• This is the first divide-and-conquer algorithm:
we solve the problem by dividing the problem
into smaller sub-problems, we recursively call
the algorithm on the sub-problems, and we
then recombine the solutions to the sub-
problems to create a solution to the overall
problem.
4
Merge-Sort
• We will recursively define merge sort as follows:
1. If the list is of size 1,
2. Otherwise, a. Divide the unsorted list into two
sub-lists,
b. Recursively call merge sort on each sub-list, and
c. Merge the two sorted sub-lists together into a
single sorted list.
5
Merge-sort is based on divide-and-conquer approach
and can be described by the following three steps:
Divide Step:
• If given array A has zero or one element, return S.
• Otherwise, divide A into two arrays, A1 and A2,
• Each containing about half of the elements of A.
Recursion Step:
• Recursively sort array A1, A2
Conquer Step:
• Combine the elements back in A by merging the sorted
arrays A1 and A2 into a sorted sequence.
Advanced Algorithms Analysis and Design
Merge-sort
Visualization of Merge-sort as Binary Tree
• We can visualize Merge-sort by means of binary tree
where each node of the tree represents a recursive call
• Each external node represents individual elements of
given array A.
• Such a tree is called Merge-sort tree.
• The heart of the Merge-sort algorithm is conquer step,
which merge two sorted sequences into a single sorted
sequence
• The merge algorithm is explained in the next
Advanced Algorithms Analysis and Design
• Sort the array [14, 10, 4, 8, 2, 12, 6, 0] in the ascending
order
Solution:
• Divide 14, 10, 4, 8, 2, 12, 6,0
14, 10, 4, 8 2, 12, 6,0
14, 10 4, 8 2, 12 6, 0
14 10 4 8 2 12 6 0
Merge-sort
Merge-sort Merge-sort
Merge-sort Merge-sort
Merge-sort Merge-sort
Advanced Algorithms Analysis and Design
Sorting Example: Divide and Conquer Rule
• Recursion and Conquer
0, 2, 4, 6, 8, 10, 12, 14
4, 8, 10, 14 0, 2, 6, 12
10, 14 4, 8 2, 12 0, 6
14 10 4 8 2 12 6 0
Merge
Merge Merge
Merge Merge
Merge Merge
Advanced Algorithms Analysis and Design
Contd..
10
Merge-sort(A, f, l)
1. if f < l
2. then m = (f + l)/2
3. Merge-sort(A, f, m)
4. Merge-sort(A, m + 1, l)
5. Merge(A, f, m, l)
Advanced Algorithms Analysis and Design
Merge-sort Algorithm
Merge(A, f, m, l)
1. T[f..l] declare temporary array of same size
2. i  f; k  f; j  m + 1 initialize integers i, j, and k
3. while (i  m) and (j  l)
4. do if (A[i]  A[j]) comparison of elements
5. then T[k++]  A[i++]
6. else T[k++]  A[j++]
7. while (i  m)
8. do T[k++]  A[i++] copy from A to T
9. while (j  l)
10. do T[k++]  A[j++] copy from A to T
11. for i  p to r
12. do A[i]  T[i] copy from T to A
Advanced Algorithms Analysis and Design
Merge-sort Algorithm
• Let T(n) be the time taken by this algorithm to sort an
array of n elements dividing A into sub-arrays A1 and A2.
• It is easy to see that the Merge (A1, A2, A) takes the
linear time. Consequently,
T(n) = T(n/2) + T(n/2) + θ(n)
T(n) = 2T (n/2) + θ(n)
• The above recurrence relation is non-homogenous and
can be solved by any of the methods
– Defining characteristics polynomial
– Substitution
– recursion tree or
– master method
Advanced Algorithms Analysis and Design
Analysis of Merge-sort Algorithm
n
n
T
n
T 
 )
2
(
.
2
)
(
2
)
2
(
.
2
)
2
( 2
n
n
T
n
T 

2
3
2
2
)
2
(
.
2
)
2
(
n
n
T
n
T 

.
.
.
2
)
2
(
.
2
)
2
( 3
4
3
n
n
T
n
T 

1
1
2
)
2
(
.
2
)
2
( 


 k
k
k
n
n
T
n
T
Advanced Algorithms Analysis and Design
Analysis: Substitution Method
n
n
n
T
n
n
T
n
T 




 )
2
(
.
2
)
(
)
2
(
.
2
)
( 2
2
n
n
n
n
T
n
T 


 )
2
(
.
2
)
( 3
3
n
n
n
T
n
T 

 )
2
(
.
2
)
( 2
2
.
.
.





times
k
k
k
n
n
n
T
n
T





 n
.
.
.
)
2
(
.
2
)
(
Advanced Algorithms Analysis and Design
Analysis of Merge-sort Algorithm
n
k
n
T
n
T k
k
.
)
2
(
.
2
)
( 

)
log
.
(
)
( 2 n
n
n
T 






times
k
k
k
n
n
n
T
n
T





 n
.
.
.
)
2
(
.
2
)
(
k
n
n k


 2
log
2
:
that
suppose
us
Let
n
n
n
n
n
T
n
n
T 2
2 log
.
log
.
)
1
(
.
)
(
Hence, 



Advanced Algorithms Analysis and Design
Analysis of Merge-sort Algorithm

More Related Content

DOC
Unit 2 in daa
DOC
algorithm Unit 2
PPTX
Data analysis and algorithms - UNIT 2.pptx
PDF
Alg_Wks1_2.pdflklokjbhvkv jv .v.vk.hk kv h/k
PPTX
Design and Analysis of Algorithm in Compter Science.pptx
PPTX
12.03.Divide-and-conquer_algorithms.pptx
PPTX
12.03.Divide-and-conquer_algorithms.pptx
PDF
Computer algorithm(Dynamic Programming).pdf
Unit 2 in daa
algorithm Unit 2
Data analysis and algorithms - UNIT 2.pptx
Alg_Wks1_2.pdflklokjbhvkv jv .v.vk.hk kv h/k
Design and Analysis of Algorithm in Compter Science.pptx
12.03.Divide-and-conquer_algorithms.pptx
12.03.Divide-and-conquer_algorithms.pptx
Computer algorithm(Dynamic Programming).pdf

Similar to Lecture -16-merge sort (slides).pptx (20)

PPTX
Matrix chain multiplication
PDF
DAA Notes.pdf
PPTX
Module 2_ Divide and Conquer Approach.pptx
PPTX
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
PPT
Tri Merge Sorting Algorithm
PPTX
Chapter 5.pptx
PDF
chapter1.pdf ......................................
PDF
Chapter One.pdf
PDF
module2_dIVIDEncONQUER_2022.pdf
PDF
Class13_Quicksort_Algorithm.pdf
PDF
Parallel Algorithms
PDF
Daa chapter 2
PPTX
sorting-160810203705.pptx
PPTX
Are there trends, changes in the mi.pptx
PPTX
daa-unit-3-greedy method
PPT
PPTX
datamining-lect8b-amachinelearninhapproach.pptx
Matrix chain multiplication
DAA Notes.pdf
Module 2_ Divide and Conquer Approach.pptx
ADA_Module 2_MN.pptx Analysis and Design of Algorithms
Book.pdf01_Intro.ppt algorithm for preperation stu used
Tri Merge Sorting Algorithm
Chapter 5.pptx
chapter1.pdf ......................................
Chapter One.pdf
module2_dIVIDEncONQUER_2022.pdf
Class13_Quicksort_Algorithm.pdf
Parallel Algorithms
Daa chapter 2
sorting-160810203705.pptx
Are there trends, changes in the mi.pptx
daa-unit-3-greedy method
datamining-lect8b-amachinelearninhapproach.pptx
Ad

Recently uploaded (20)

PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPTX
Introduction to machine learning and Linear Models
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PDF
Mega Projects Data Mega Projects Data
PDF
annual-report-2024-2025 original latest.
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPT
Quality review (1)_presentation of this 21
PDF
Business Analytics and business intelligence.pdf
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Introduction to machine learning and Linear Models
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Mega Projects Data Mega Projects Data
annual-report-2024-2025 original latest.
Clinical guidelines as a resource for EBP(1).pdf
Introduction to Knowledge Engineering Part 1
climate analysis of Dhaka ,Banglades.pptx
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Miokarditis (Inflamasi pada Otot Jantung)
Business Acumen Training GuidePresentation.pptx
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Quality review (1)_presentation of this 21
Business Analytics and business intelligence.pdf
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
Ad

Lecture -16-merge sort (slides).pptx

  • 1. Merge sort Analysis and complexity Design and Analysis of Algorithms 1
  • 2. Divide and Conquer Approach Step 1: • If the problem size is small, solve this problem directly • Otherwise, split the original problem into 2 or more sub-problems with almost equal sizes. Step 2: • Recursively solve these sub-problems by applying this algorithm. Step 3: • Merge the solutions of the sub- problems into a solution of the original problem. 2
  • 3. • Time complexity: – where S(n) is time for splitting – M(n) is time for merging – b and c are constants Example • Binary search • Quick sort • Merge sort T(n)=    2T(n/2) + S(n) + M(n) b , n  c , n < c Advanced Algorithms Analysis and Design Time Complexity of General Algorithms
  • 4. Merge-Sort • This is the first divide-and-conquer algorithm: we solve the problem by dividing the problem into smaller sub-problems, we recursively call the algorithm on the sub-problems, and we then recombine the solutions to the sub- problems to create a solution to the overall problem. 4
  • 5. Merge-Sort • We will recursively define merge sort as follows: 1. If the list is of size 1, 2. Otherwise, a. Divide the unsorted list into two sub-lists, b. Recursively call merge sort on each sub-list, and c. Merge the two sorted sub-lists together into a single sorted list. 5
  • 6. Merge-sort is based on divide-and-conquer approach and can be described by the following three steps: Divide Step: • If given array A has zero or one element, return S. • Otherwise, divide A into two arrays, A1 and A2, • Each containing about half of the elements of A. Recursion Step: • Recursively sort array A1, A2 Conquer Step: • Combine the elements back in A by merging the sorted arrays A1 and A2 into a sorted sequence. Advanced Algorithms Analysis and Design Merge-sort
  • 7. Visualization of Merge-sort as Binary Tree • We can visualize Merge-sort by means of binary tree where each node of the tree represents a recursive call • Each external node represents individual elements of given array A. • Such a tree is called Merge-sort tree. • The heart of the Merge-sort algorithm is conquer step, which merge two sorted sequences into a single sorted sequence • The merge algorithm is explained in the next Advanced Algorithms Analysis and Design
  • 8. • Sort the array [14, 10, 4, 8, 2, 12, 6, 0] in the ascending order Solution: • Divide 14, 10, 4, 8, 2, 12, 6,0 14, 10, 4, 8 2, 12, 6,0 14, 10 4, 8 2, 12 6, 0 14 10 4 8 2 12 6 0 Merge-sort Merge-sort Merge-sort Merge-sort Merge-sort Merge-sort Merge-sort Advanced Algorithms Analysis and Design Sorting Example: Divide and Conquer Rule
  • 9. • Recursion and Conquer 0, 2, 4, 6, 8, 10, 12, 14 4, 8, 10, 14 0, 2, 6, 12 10, 14 4, 8 2, 12 0, 6 14 10 4 8 2 12 6 0 Merge Merge Merge Merge Merge Merge Merge Advanced Algorithms Analysis and Design Contd..
  • 10. 10
  • 11. Merge-sort(A, f, l) 1. if f < l 2. then m = (f + l)/2 3. Merge-sort(A, f, m) 4. Merge-sort(A, m + 1, l) 5. Merge(A, f, m, l) Advanced Algorithms Analysis and Design Merge-sort Algorithm
  • 12. Merge(A, f, m, l) 1. T[f..l] declare temporary array of same size 2. i  f; k  f; j  m + 1 initialize integers i, j, and k 3. while (i  m) and (j  l) 4. do if (A[i]  A[j]) comparison of elements 5. then T[k++]  A[i++] 6. else T[k++]  A[j++] 7. while (i  m) 8. do T[k++]  A[i++] copy from A to T 9. while (j  l) 10. do T[k++]  A[j++] copy from A to T 11. for i  p to r 12. do A[i]  T[i] copy from T to A Advanced Algorithms Analysis and Design Merge-sort Algorithm
  • 13. • Let T(n) be the time taken by this algorithm to sort an array of n elements dividing A into sub-arrays A1 and A2. • It is easy to see that the Merge (A1, A2, A) takes the linear time. Consequently, T(n) = T(n/2) + T(n/2) + θ(n) T(n) = 2T (n/2) + θ(n) • The above recurrence relation is non-homogenous and can be solved by any of the methods – Defining characteristics polynomial – Substitution – recursion tree or – master method Advanced Algorithms Analysis and Design Analysis of Merge-sort Algorithm
  • 14. n n T n T   ) 2 ( . 2 ) ( 2 ) 2 ( . 2 ) 2 ( 2 n n T n T   2 3 2 2 ) 2 ( . 2 ) 2 ( n n T n T   . . . 2 ) 2 ( . 2 ) 2 ( 3 4 3 n n T n T   1 1 2 ) 2 ( . 2 ) 2 (     k k k n n T n T Advanced Algorithms Analysis and Design Analysis: Substitution Method
  • 15. n n n T n n T n T       ) 2 ( . 2 ) ( ) 2 ( . 2 ) ( 2 2 n n n n T n T     ) 2 ( . 2 ) ( 3 3 n n n T n T    ) 2 ( . 2 ) ( 2 2 . . .      times k k k n n n T n T       n . . . ) 2 ( . 2 ) ( Advanced Algorithms Analysis and Design Analysis of Merge-sort Algorithm
  • 16. n k n T n T k k . ) 2 ( . 2 ) (   ) log . ( ) ( 2 n n n T        times k k k n n n T n T       n . . . ) 2 ( . 2 ) ( k n n k    2 log 2 : that suppose us Let n n n n n T n n T 2 2 log . log . ) 1 ( . ) ( Hence,     Advanced Algorithms Analysis and Design Analysis of Merge-sort Algorithm