SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

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

Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmAshim Sikder
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptxTekle12
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................nourhandardeer3
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdfabay golla
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfShiwani Gupta
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0BG Java EE Course
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesNirmalavenkatachalam
 
Module 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsModule 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsCool Guy
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2Deepak John
 
DAA - chapter 1.pdf
DAA - chapter 1.pdfDAA - chapter 1.pdf
DAA - chapter 1.pdfASMAALWADEE2
 

Similar a 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
 

Último

Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...shivangimorya083
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...SUHANI PANDEY
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxMohammedJunaid861692
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Delhi Call girls
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceDelhi Call girls
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023ymrp368
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfadriantubila
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxolyaivanovalion
 

Último (20)

Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
 
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort ServiceBDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
BDSM⚡Call Girls in Mandawali Delhi >༒8448380779 Escort Service
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptx
 

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