SlideShare a Scribd company logo
1 of 16
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

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

Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting Algorithm
Ashim Sikder
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 

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

Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting Algorithm
 
sorting
sortingsorting
sorting
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Parallel Algorithms
Parallel AlgorithmsParallel Algorithms
Parallel Algorithms
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Lec35
Lec35Lec35
Lec35
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
 
Ch07 linearspacealignment
Ch07 linearspacealignmentCh07 linearspacealignment
Ch07 linearspacealignment
 
Module 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsModule 2 Design Analysis and Algorithms
Module 2 Design Analysis and Algorithms
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
Ada notes
Ada notesAda notes
Ada notes
 
DAA - chapter 1.pdf
DAA - chapter 1.pdfDAA - chapter 1.pdf
DAA - chapter 1.pdf
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 

Recently uploaded

Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit RiyadhCytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Abortion pills in Riyadh +966572737505 get cytotec
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
nirzagarg
 
PLE-statistics document for primary schs
PLE-statistics document for primary schsPLE-statistics document for primary schs
PLE-statistics document for primary schs
cnajjemba
 
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
vexqp
 
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
nirzagarg
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Klinik kandungan
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
gajnagarg
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
nirzagarg
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
vexqp
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
nirzagarg
 

Recently uploaded (20)

Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit RiyadhCytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
 
Sequential and reinforcement learning for demand side management by Margaux B...
Sequential and reinforcement learning for demand side management by Margaux B...Sequential and reinforcement learning for demand side management by Margaux B...
Sequential and reinforcement learning for demand side management by Margaux B...
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubai
 
Data Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdfData Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdf
 
PLE-statistics document for primary schs
PLE-statistics document for primary schsPLE-statistics document for primary schs
PLE-statistics document for primary schs
 
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
 
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
 
The-boAt-Story-Navigating-the-Waves-of-Innovation.pptx
The-boAt-Story-Navigating-the-Waves-of-Innovation.pptxThe-boAt-Story-Navigating-the-Waves-of-Innovation.pptx
The-boAt-Story-Navigating-the-Waves-of-Innovation.pptx
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In dimapur [ 7014168258 ] Call Me For Genuine Models W...
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
 
Harnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptxHarnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptx
 
Capstone in Interprofessional Informatic // IMPACT OF COVID 19 ON EDUCATION
Capstone in Interprofessional Informatic  // IMPACT OF COVID 19 ON EDUCATIONCapstone in Interprofessional Informatic  // IMPACT OF COVID 19 ON EDUCATION
Capstone in Interprofessional Informatic // IMPACT OF COVID 19 ON EDUCATION
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
 
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With OrangePredicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
Predicting HDB Resale Prices - Conducting Linear Regression Analysis With Orange
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
 

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