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

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
 
sorting
sortingsorting
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
Tekle12
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................
nourhandardeer3
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
abay golla
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
Shiwani Gupta
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Parallel Algorithms
Parallel AlgorithmsParallel Algorithms
Parallel Algorithms
Dr Sandeep Kumar Poonia
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
B.Kirron Reddi
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
VarchasvaTiwari2
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Lec35
Lec35Lec35
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
BG Java EE Course
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
Nirmalavenkatachalam
 
Ch07 linearspacealignment
Ch07 linearspacealignmentCh07 linearspacealignment
Ch07 linearspacealignment
BioinformaticsInstitute
 
Module 2 Design Analysis and Algorithms
Module 2 Design Analysis and AlgorithmsModule 2 Design Analysis and Algorithms
Module 2 Design Analysis and Algorithms
Cool Guy
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
Deepak John
 
Ada notes
Ada notesAda notes
DAA - chapter 1.pdf
DAA - chapter 1.pdfDAA - chapter 1.pdf
DAA - chapter 1.pdf
ASMAALWADEE2
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
B.Kirron Reddi
 

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

一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
74nqk8xf
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
GetInData
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
Social Samosa
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
v3tuleee
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
jerlynmaetalle
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
vikram sood
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
ahzuo
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
manishkhaire30
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
v7oacc3l
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
sameer shah
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
bopyb
 
State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023
kuntobimo2016
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
Bill641377
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
Sachin Paul
 
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
g4dpvqap0
 

Recently uploaded (20)

一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
一比一原版(牛布毕业证书)牛津布鲁克斯大学毕业证如何办理
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
 
State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...Population Growth in Bataan: The effects of population growth around rural pl...
Population Growth in Bataan: The effects of population growth around rural pl...
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
 
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
 

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