SlideShare a Scribd company logo
DESIGN AND ANALYSIS
OF ALGORITHMS
Merge Sort
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 2, Module 5
O(n2) sorting algorithms
Selection sort and insertion sort are both O(n2)
O(n2) sorting is infeasible for n over 100000
A different strategy?
Divide array in two equal parts
Separately sort left and right half
Combine the two sorted halves to get the full array
sorted
Combining sorted lists
Given two sorted lists A and B, combine into a
sorted list C
Compare first element of A and B
Move it into C
Repeat until all elements in A and B are over
Merging A and B
Merging two sorted lists
32 74 89
21 55 64
Merging two sorted lists
32 74 89
21 55 64
21
Merging two sorted lists
32 74 89
21 55 64
21 32
Merging two sorted lists
32 74 89
21 55 64
21 32 55
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64 74
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64 74 89
Merge Sort
Sort A[0] to A[n/2-1]
Sort A[n/2] to A[n-1]
Merge sorted halves into B[0..n-1]
How do we sort the halves?
Recursively, using the same strategy!
Merge Sort
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
Merge Sort
43 32 22 78 63 57 91 13
63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
22 32 43 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
22 32 43 78 13 57 63 91
Merge Sort
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
22 32 43 78 13 57 63 91
13 22 32 43 57 63 78 91
Divide and conquer
Break up problem into disjoint parts
Solve each part separately
Combine the solutions efficiently
Merging sorted lists
Combine two sorted lists A and B into C
If A is empty, copy B into C
If B is empty, copy A into C
Otherwise, compare first element of A and B and
move the smaller of the two into C
Repeat until all elements in A and B have been
moved
Merging
function Merge(A,m,B,n,C)
// Merge A[0..m-1], B[0..n-1] into C[0..m+n-1]
i = 0; j = 0; k = 0;
// Current positions in A,B,C respectively
while (k < m+n)
// Case 0: One of the two lists is empty
if (i==m) {j++; k++;}
if (j==n) {i++; k++;}
// Case 1: Move head of A into C
if (A[i] <= B[j]) { C[k] = B[j]; j++; k++;}
// Case 2: Move head of B into C
if (A[i] > B[j]) {C[k] = B[j]; j++; k++;}
Merge Sort
To sort A[0..n-1] into B[0..n-1]
If n is 1, nothing to be done
Otherwise
Sort A[0..n/2-1] into L (left)
Sort A[n/2..n-1] into R (right)
Merge L and R into B
Merge Sort
function MergeSort(A,left,right,B)
// Sort the segment A[left..right-1] into B
if (right - left == 1) // Base case
B[0] = A[left]
if (right - left > 1) // Recursive call
mid = (left+right)/2
MergeSort(A,left,mid,L)
MergeSort(A,mid,right,R)
Merge(L,mid-left,R,right-mid,B)

More Related Content

Similar to merge sort

9.matrices and transformation Further Mathematics Zimbabwe Zimsec Cambridge
9.matrices and transformation   Further Mathematics Zimbabwe Zimsec Cambridge9.matrices and transformation   Further Mathematics Zimbabwe Zimsec Cambridge
9.matrices and transformation Further Mathematics Zimbabwe Zimsec Cambridge
alproelearning
 
Ma3bfet par 10.5 31 julie 2014
Ma3bfet par 10.5 31 julie 2014Ma3bfet par 10.5 31 julie 2014
Ma3bfet par 10.5 31 julie 2014
Celumusa Godfrey Nkosi
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
Krish_ver2
 
insertion sort-new.pdf
insertion sort-new.pdfinsertion sort-new.pdf
insertion sort-new.pdf
ObedurRahman1
 
Chapter 03-group-theory (1)
Chapter 03-group-theory (1)Chapter 03-group-theory (1)
Chapter 03-group-theory (1)
한석 나
 
MATRICES AND CALCULUS.pptx
MATRICES AND CALCULUS.pptxMATRICES AND CALCULUS.pptx
MATRICES AND CALCULUS.pptx
massm99m
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika
MamaMa28
 
Determinants
DeterminantsDeterminants
Determinants
Joey Valdriz
 
Elasticity, Plasticity and elastic plastic analysis
Elasticity, Plasticity and elastic plastic analysisElasticity, Plasticity and elastic plastic analysis
Elasticity, Plasticity and elastic plastic analysis
JAGARANCHAKMA2
 
24 Cs146 Jc Merge
24 Cs146 Jc Merge24 Cs146 Jc Merge
24 Cs146 Jc Merge
Renu Kewalramani
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
kalyanineve
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
maths revision to help you
 maths revision to help you maths revision to help you
maths revision to help you
JakeHornsby3219
 
Exercises in Linear Algebra - Matrices and determinants
Exercises in Linear Algebra - Matrices and determinantsExercises in Linear Algebra - Matrices and determinants
Exercises in Linear Algebra - Matrices and determinants
Maths Tutoring
 
determinants-160504230830.pdf
determinants-160504230830.pdfdeterminants-160504230830.pdf
determinants-160504230830.pdf
Praveen Kumar Verma PMP
 
determinants-160504230830_repaired.pdf
determinants-160504230830_repaired.pdfdeterminants-160504230830_repaired.pdf
determinants-160504230830_repaired.pdf
TGBSmile
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
Chapter-2.pptx
Chapter-2.pptxChapter-2.pptx
Chapter-2.pptx
selemonGamo
 
Matrix.pptx
Matrix.pptxMatrix.pptx
Matrix.pptx
SKalyani_cbe
 
Bba i-bm-u-2- matrix -
Bba i-bm-u-2- matrix -Bba i-bm-u-2- matrix -
Bba i-bm-u-2- matrix -
Rai University
 

Similar to merge sort (20)

9.matrices and transformation Further Mathematics Zimbabwe Zimsec Cambridge
9.matrices and transformation   Further Mathematics Zimbabwe Zimsec Cambridge9.matrices and transformation   Further Mathematics Zimbabwe Zimsec Cambridge
9.matrices and transformation Further Mathematics Zimbabwe Zimsec Cambridge
 
Ma3bfet par 10.5 31 julie 2014
Ma3bfet par 10.5 31 julie 2014Ma3bfet par 10.5 31 julie 2014
Ma3bfet par 10.5 31 julie 2014
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
 
insertion sort-new.pdf
insertion sort-new.pdfinsertion sort-new.pdf
insertion sort-new.pdf
 
Chapter 03-group-theory (1)
Chapter 03-group-theory (1)Chapter 03-group-theory (1)
Chapter 03-group-theory (1)
 
MATRICES AND CALCULUS.pptx
MATRICES AND CALCULUS.pptxMATRICES AND CALCULUS.pptx
MATRICES AND CALCULUS.pptx
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika
 
Determinants
DeterminantsDeterminants
Determinants
 
Elasticity, Plasticity and elastic plastic analysis
Elasticity, Plasticity and elastic plastic analysisElasticity, Plasticity and elastic plastic analysis
Elasticity, Plasticity and elastic plastic analysis
 
24 Cs146 Jc Merge
24 Cs146 Jc Merge24 Cs146 Jc Merge
24 Cs146 Jc Merge
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
maths revision to help you
 maths revision to help you maths revision to help you
maths revision to help you
 
Exercises in Linear Algebra - Matrices and determinants
Exercises in Linear Algebra - Matrices and determinantsExercises in Linear Algebra - Matrices and determinants
Exercises in Linear Algebra - Matrices and determinants
 
determinants-160504230830.pdf
determinants-160504230830.pdfdeterminants-160504230830.pdf
determinants-160504230830.pdf
 
determinants-160504230830_repaired.pdf
determinants-160504230830_repaired.pdfdeterminants-160504230830_repaired.pdf
determinants-160504230830_repaired.pdf
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 
Chapter-2.pptx
Chapter-2.pptxChapter-2.pptx
Chapter-2.pptx
 
Matrix.pptx
Matrix.pptxMatrix.pptx
Matrix.pptx
 
Bba i-bm-u-2- matrix -
Bba i-bm-u-2- matrix -Bba i-bm-u-2- matrix -
Bba i-bm-u-2- matrix -
 

Recently uploaded

Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
kalichargn70th171
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
Yara Milbes
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
aisafed42
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLESINTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
anfaltahir1010
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 

Recently uploaded (20)

Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLESINTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 

merge sort

  • 1. DESIGN AND ANALYSIS OF ALGORITHMS Merge Sort MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE http://www.cmi.ac.in/~madhavan NPTEL MOOC,JAN-FEB 2015 Week 2, Module 5
  • 2. O(n2) sorting algorithms Selection sort and insertion sort are both O(n2) O(n2) sorting is infeasible for n over 100000
  • 3. A different strategy? Divide array in two equal parts Separately sort left and right half Combine the two sorted halves to get the full array sorted
  • 4. Combining sorted lists Given two sorted lists A and B, combine into a sorted list C Compare first element of A and B Move it into C Repeat until all elements in A and B are over Merging A and B
  • 5. Merging two sorted lists 32 74 89 21 55 64
  • 6. Merging two sorted lists 32 74 89 21 55 64 21
  • 7. Merging two sorted lists 32 74 89 21 55 64 21 32
  • 8. Merging two sorted lists 32 74 89 21 55 64 21 32 55
  • 9. Merging two sorted lists 32 74 89 21 55 64 21 32 55 64
  • 10. Merging two sorted lists 32 74 89 21 55 64 21 32 55 64 74
  • 11. Merging two sorted lists 32 74 89 21 55 64 21 32 55 64 74 89
  • 12. Merge Sort Sort A[0] to A[n/2-1] Sort A[n/2] to A[n-1] Merge sorted halves into B[0..n-1] How do we sort the halves? Recursively, using the same strategy!
  • 13. Merge Sort 43 32 22 78 63 57 91 13
  • 14. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78
  • 15. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13
  • 16. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78
  • 17. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13
  • 18. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32
  • 19. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78
  • 20. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57
  • 21. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13
  • 22. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 22 78 63 57 91 13 43 32 22 78 63 57 91 13 32 43
  • 23. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 63 57 91 13 43 32 22 78 63 57 91 13 32 43 22 78
  • 24. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 91 13 43 32 22 78 63 57 91 13 32 43 22 78 57 63
  • 25. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 32 43 22 78 57 63 13 91
  • 26. Merge Sort 43 32 22 78 63 57 91 13 63 57 91 13 43 32 22 78 63 57 91 13 32 43 22 78 57 63 13 91 22 32 43 78
  • 27. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 32 43 22 78 57 63 13 91 22 32 43 78 13 57 63 91
  • 28. Merge Sort 43 32 22 78 63 57 91 13 32 43 22 78 57 63 13 91 22 32 43 78 13 57 63 91 13 22 32 43 57 63 78 91
  • 29. Divide and conquer Break up problem into disjoint parts Solve each part separately Combine the solutions efficiently
  • 30. Merging sorted lists Combine two sorted lists A and B into C If A is empty, copy B into C If B is empty, copy A into C Otherwise, compare first element of A and B and move the smaller of the two into C Repeat until all elements in A and B have been moved
  • 31. Merging function Merge(A,m,B,n,C) // Merge A[0..m-1], B[0..n-1] into C[0..m+n-1] i = 0; j = 0; k = 0; // Current positions in A,B,C respectively while (k < m+n) // Case 0: One of the two lists is empty if (i==m) {j++; k++;} if (j==n) {i++; k++;} // Case 1: Move head of A into C if (A[i] <= B[j]) { C[k] = B[j]; j++; k++;} // Case 2: Move head of B into C if (A[i] > B[j]) {C[k] = B[j]; j++; k++;}
  • 32. Merge Sort To sort A[0..n-1] into B[0..n-1] If n is 1, nothing to be done Otherwise Sort A[0..n/2-1] into L (left) Sort A[n/2..n-1] into R (right) Merge L and R into B
  • 33. Merge Sort function MergeSort(A,left,right,B) // Sort the segment A[left..right-1] into B if (right - left == 1) // Base case B[0] = A[left] if (right - left > 1) // Recursive call mid = (left+right)/2 MergeSort(A,left,mid,L) MergeSort(A,mid,right,R) Merge(L,mid-left,R,right-mid,B)