SlideShare a Scribd company logo
MERGE SORT -
HOW RECURSION WORKS
10 4 126152
2 4 6 10 12 15
Dr.S.Lovelyn Rose
PSGCollege ofTechnology
Coimbatore, India
Splitting
Solve subproblems
Combine solution
ALGORITHM
MergeSort(low,high)
{
if(low<high)
{
mid=(low+high)/2
MergeSort(low,mid)
MergeSort(mid+1,high)
Merge(low,mid,high)
}
}
Merge(low,mid,high)
{
h=low; i=low; j=mid+1
while((h<=mid) and (j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h]
h=h+1
}
else
{
b[i]=a[j]
j=j+1
}
i=i+1
}//End of while
temp array 2nd sub array1st sub array
temp to original array
for k=low to high
a[k]=b[k]
}
If(h>mid)
{
for k=j to high
{
b[i]=a[k]
i=i+1
}
}
else
{
for k=h to mid
{
b[i]=a[k]
i=i+1
}
}
Recursion Order
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
3 8 4 51 72 9
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
1
2
3 4
5
6
7 8
9
10
11
12
13 14
15
16
17 18
19
20
21
Note :The numbers in red help to understand the order in which recursion takes place
Formulating the Recurrence Relation
T(n)=2T(n/2)+(n-1)
Size of a sublist
Number of sublists
(n-1)
 Number of times basic operation is performed when a function is called
 Inside Merge() a minimum of (n-1) comparisons is performed.
Eg.
 If 1 element no comparison
 If 2 elements->max 1 comparison
 If 4 elements->3 comparisons
Solving Recurrence
MastersTheorem
a=2,b=2,f(n)=n-1
log b a=1
By case 2,f(n)=Ѳ(n1 )=O(n)
T(n)= Ѳ(n log n)
By Substitution Method
T(n)=2T(n/2)+(n-1)
=2(2T(n/22 )+((n/2)-1))+(n-1)
=22 T(n/22 )+(n-2)+(n-1)
=22 (2T(n/23 )+((n/4)-1))+(2n-(1+2))
=23T (n/23 )+3n-(1+2+22 )
.
.
=2k T(n/2k )+kn-(1+2+…+2k-1 )
Let n=2k
=2k T(1 )+n log n-(2k -1 )
=n log n-n+1
= Ѳ(n log n)
Space Complexity
= n+n = 2n = Ѳ(n)
Additional array ‘b’ of size ‘n’
Input Array of size ‘n’
My Blogs
http://datastructuresinterview.blogspot.in/
http://talkcoimbatore.blogspot.in/
http://simpletechnical.blogspot.in/

More Related Content

PDF
0-1 knapsack problem
PPT
Section 5.5 properties of logarithms
PPT
Section 5.4 logarithmic functions
PPTX
Bohrn haber cycle of NaCl
PDF
Energy considerations in ionic bonding, lattice energy and solvation energy
PPT
Section 5.6 logarithmic and exponential equations
PDF
IRJET - Some Results on Fuzzy Semi-Super Modular Lattices
DOCX
0-1 knapsack problem
Section 5.5 properties of logarithms
Section 5.4 logarithmic functions
Bohrn haber cycle of NaCl
Energy considerations in ionic bonding, lattice energy and solvation energy
Section 5.6 logarithmic and exponential equations
IRJET - Some Results on Fuzzy Semi-Super Modular Lattices

What's hot (7)

PPT
Section 3.1 linear functions and their properties
PPT
Section 2.2 the graph of a function
PDF
Metrics for generativemodels
PDF
F04573843
PDF
Intro to Quantitative Investment (Lecture 4 of 6)
PDF
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural Networks
PDF
Error Estimates for Multi-Penalty Regularization under General Source Condition
Section 3.1 linear functions and their properties
Section 2.2 the graph of a function
Metrics for generativemodels
F04573843
Intro to Quantitative Investment (Lecture 4 of 6)
Paper Study: OptNet: Differentiable Optimization as a Layer in Neural Networks
Error Estimates for Multi-Penalty Regularization under General Source Condition
Ad

More from Lovelyn Rose (6)

PPTX
Deep learning simplified
PPTX
Problem solving
PPTX
Placement oriented data structures
PPTX
Linked list
PPTX
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
PPTX
Linked list without animation
Deep learning simplified
Problem solving
Placement oriented data structures
Linked list
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Linked list without animation
Ad

Recently uploaded (20)

PDF
Trump Administration's workforce development strategy
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
1_English_Language_Set_2.pdf probationary
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
Empowerment Technology for Senior High School Guide
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Trump Administration's workforce development strategy
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
1_English_Language_Set_2.pdf probationary
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
B.Sc. DS Unit 2 Software Engineering.pptx
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
What if we spent less time fighting change, and more time building what’s rig...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
History, Philosophy and sociology of education (1).pptx
A powerpoint presentation on the Revised K-10 Science Shaping Paper
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Empowerment Technology for Senior High School Guide
FORM 1 BIOLOGY MIND MAPS and their schemes
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS

Mergesort without Animation