SlideShare a Scribd company logo
1 of 26
Merge Sort Implementation
Introduction to Group members
Nazmul Hasan Rupu
bsse 1034
Sadikul Haque Sadi
bsse 1003
Merge Sort
 In merge sort algorithm , we use the
concept of divide and conquer.
 We divide the array into two parts ,then
sort them and merge them to get sorted
array.
 This sorting method is an
implementation of recursive function.
Merge Sort
 We take an array and keep dividing it
from middle till we get only one element
in each part(sub-array) .
 Then we sort the sub-arrays and merge
them to get the final sorted array.
Example
Let’s consider an array of 6 elements.
5 3 2 1 4 6
Arrange them in ascending order using
merge sort algorithm.
Array index
Array elements
0 1 2 3 4 5
5 3 2 1 4 6
Begin
Starting index
End
Last index
Is Size >1 ?
yes
Divide the array into 2 part
Mid = Size/2
Mid = 6/2 = 3
Size= 6
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
5 3 2
We will consider the left sub-array
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
5 3 2
Is Size > 1 ?
Size=3
yes
Divide the array into 2 part
Mid = Size/2
Mid = 3/2 = 1
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
5 3 2
0
5
Is Size > 1 ?
no
Size=1
As there is only one
element in the array
we now go to the
right sub array
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
5 3 2
0
5
0 1
3 2
Size =2
Is Size > 1 ?
yes
Divide the array into 2 part
Mid = Size/2
Mid = 2/2 = 1
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
5 3 2
0
5
0 1
3 2
0
3
1
2
As there is only
one element in
the array we now
go to the right
sub array
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
5 3 2
0
5
0 1
3 2
0
3
1
2
Both the right sub-
array and left sub-
array have one
element
So we can now
sort them and
merge them
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
5 3 2
0
5
0 1
3 2
0
3
1
2
0 1
2 2
0 1
2 3
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
5 3 2
0
5
0 1
2 3
0 1 2
2 3 2
0 1 2
2 3 2
0 1 2
2 3 5
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
2 3 5
The left sub-array is
sorted now we will go
to right sub-array
0 1 2
1 4 6
Now we will follow
the same way as we
followed so far
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
2 3 5
0 1 2
1 4 6
0
1
0 1
4 6
0
4
0
6
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
2 3 5
0 1 2
1 4 6
0
1
0 1
4 6
0
4
0
6
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
2 3 5
0 1 2
1 4 6
0
1
0 1
4 6
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
2 3 5
0 1 2
1 4 6
The left and right array are sortedWe can now merge them to get
sorted array in ascending
order
0 1 2 3 4 5
5 3 2 1 4 6
0 1 2
2 3 5
0 1 2
1 4 6
0 1 2 3 4 5
1 3 2 1 4 6
0 1 2
2 3 5
0 1 2
1 4 6
0 1 2 3 4 5
1 2 2 1 4 6
0 1 2
2 3 5
0 1 2
1 4 6
0 1 2 3 4 5
1 2 3 1 4 6
0 1 2
2 3 5
0 1 2
1 4 6
0 1 2 3 4 5
1 2 3 4 4 6
0 1 2
2 3 5
0 1 2
1 4 6
0 1 2 3 4 5
1 2 3 4 5 6
0 1 2
2 3 5
0 1 2
1 4 6
0 1 2 3 4 5
1 2 3 4 5 6
The array is now sorted ! ! !
Merge sort implementation

More Related Content

What's hot

3.2 use parallel lines and transversals
3.2 use parallel lines and transversals3.2 use parallel lines and transversals
3.2 use parallel lines and transversalsmasljr
 
Adding and subtracting positive and negative rational number notes 1
Adding and subtracting positive and negative rational number notes 1Adding and subtracting positive and negative rational number notes 1
Adding and subtracting positive and negative rational number notes 1citlaly9
 
3.2 use parallel lines and transversals
3.2 use parallel lines and transversals3.2 use parallel lines and transversals
3.2 use parallel lines and transversalsdetwilerr
 
Python week4-lecture1-handout
Python week4-lecture1-handoutPython week4-lecture1-handout
Python week4-lecture1-handoutAnmolTwickley
 
Parallel Lines Angles
Parallel Lines AnglesParallel Lines Angles
Parallel Lines AnglesDyan Aniol
 
Math chapter 3 multiplying exponential expressions
Math chapter 3 multiplying exponential expressionsMath chapter 3 multiplying exponential expressions
Math chapter 3 multiplying exponential expressionsJaredSalvan
 
Math chapter 3 dividing exponential expressions
Math chapter 3 dividing exponential expressionsMath chapter 3 dividing exponential expressions
Math chapter 3 dividing exponential expressionsJaredSalvan
 
3rd Test - Slope and parallel lines
3rd Test - Slope and parallel lines3rd Test - Slope and parallel lines
3rd Test - Slope and parallel linesBrandeis High School
 
Rationalizing the Denominator of a Radical Expression
Rationalizing the Denominator of a Radical ExpressionRationalizing the Denominator of a Radical Expression
Rationalizing the Denominator of a Radical ExpressionREYBETH RACELIS
 
Unit 6 review
Unit 6 reviewUnit 6 review
Unit 6 reviewmlabuski
 
Unit 1 BP801T a introduction mean median mode
Unit 1 BP801T a introduction mean median mode Unit 1 BP801T a introduction mean median mode
Unit 1 BP801T a introduction mean median mode ashish7sattee
 
Roots and radical expressions
Roots and radical expressionsRoots and radical expressions
Roots and radical expressionsholmsted
 
Mean, Median And Mode
Mean, Median And ModeMean, Median And Mode
Mean, Median And Modestanhopekris
 
Exponents Rules
Exponents RulesExponents Rules
Exponents RulesBigPassy2
 

What's hot (20)

3.2 use parallel lines and transversals
3.2 use parallel lines and transversals3.2 use parallel lines and transversals
3.2 use parallel lines and transversals
 
Lesson 1 student notes
Lesson 1 student notesLesson 1 student notes
Lesson 1 student notes
 
Adding and subtracting positive and negative rational number notes 1
Adding and subtracting positive and negative rational number notes 1Adding and subtracting positive and negative rational number notes 1
Adding and subtracting positive and negative rational number notes 1
 
Rational Expressions
Rational ExpressionsRational Expressions
Rational Expressions
 
Content schedule bt
Content schedule btContent schedule bt
Content schedule bt
 
3.2 use parallel lines and transversals
3.2 use parallel lines and transversals3.2 use parallel lines and transversals
3.2 use parallel lines and transversals
 
Python week4-lecture1-handout
Python week4-lecture1-handoutPython week4-lecture1-handout
Python week4-lecture1-handout
 
Parallel Lines Angles
Parallel Lines AnglesParallel Lines Angles
Parallel Lines Angles
 
Math chapter 3 multiplying exponential expressions
Math chapter 3 multiplying exponential expressionsMath chapter 3 multiplying exponential expressions
Math chapter 3 multiplying exponential expressions
 
Math chapter 3 dividing exponential expressions
Math chapter 3 dividing exponential expressionsMath chapter 3 dividing exponential expressions
Math chapter 3 dividing exponential expressions
 
Understanding exponents
Understanding exponentsUnderstanding exponents
Understanding exponents
 
3rd Test - Slope and parallel lines
3rd Test - Slope and parallel lines3rd Test - Slope and parallel lines
3rd Test - Slope and parallel lines
 
L 50
L 50L 50
L 50
 
Rationalizing the Denominator of a Radical Expression
Rationalizing the Denominator of a Radical ExpressionRationalizing the Denominator of a Radical Expression
Rationalizing the Denominator of a Radical Expression
 
Unit 6 review
Unit 6 reviewUnit 6 review
Unit 6 review
 
Unit 1 BP801T a introduction mean median mode
Unit 1 BP801T a introduction mean median mode Unit 1 BP801T a introduction mean median mode
Unit 1 BP801T a introduction mean median mode
 
Roots and radical expressions
Roots and radical expressionsRoots and radical expressions
Roots and radical expressions
 
Mean, Median And Mode
Mean, Median And ModeMean, Median And Mode
Mean, Median And Mode
 
Adding similar fractions
Adding similar fractionsAdding similar fractions
Adding similar fractions
 
Exponents Rules
Exponents RulesExponents Rules
Exponents Rules
 

Similar to Merge sort implementation

Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...BhumikaBiyani1
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfArjunSingh81957
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityMotaleb Hossen Manik
 
Quick sort-170316064200
Quick sort-170316064200Quick sort-170316064200
Quick sort-170316064200ifeanyiokeke3
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data StructureJeanie Arnoco
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Flynce Miguel
 
K-12 Mathematics lesson Presentation.pptx
K-12 Mathematics lesson Presentation.pptxK-12 Mathematics lesson Presentation.pptx
K-12 Mathematics lesson Presentation.pptxchacha987301
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printAbdii Rashid
 
Section 8: Symmetric Groups
Section 8: Symmetric GroupsSection 8: Symmetric Groups
Section 8: Symmetric GroupsKevin Johnson
 
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)Jinho Choi
 
Coordinate Geometry
Coordinate GeometryCoordinate Geometry
Coordinate Geometryhiratufail
 
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sortNauman Ali
 
Coordinate Geometry
Coordinate GeometryCoordinate Geometry
Coordinate GeometryManisha Keim
 

Similar to Merge sort implementation (20)

Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...Sorting in data structures and algorithms , it has all the necessary points t...
Sorting in data structures and algorithms , it has all the necessary points t...
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
 
Sorting
SortingSorting
Sorting
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexity
 
Quick sort-170316064200
Quick sort-170316064200Quick sort-170316064200
Quick sort-170316064200
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
K-12 Mathematics lesson Presentation.pptx
K-12 Mathematics lesson Presentation.pptxK-12 Mathematics lesson Presentation.pptx
K-12 Mathematics lesson Presentation.pptx
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Sorting
SortingSorting
Sorting
 
Lattice multiplication
Lattice multiplicationLattice multiplication
Lattice multiplication
 
Algo PPT.pdf
Algo PPT.pdfAlgo PPT.pdf
Algo PPT.pdf
 
Section 8: Symmetric Groups
Section 8: Symmetric GroupsSection 8: Symmetric Groups
Section 8: Symmetric Groups
 
Merge sort
Merge sortMerge sort
Merge sort
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)CS253: Divide & Conquer Sort (2019)
CS253: Divide & Conquer Sort (2019)
 
Coordinate Geometry
Coordinate GeometryCoordinate Geometry
Coordinate Geometry
 
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sort
 
Quick_sort1.pptx
Quick_sort1.pptxQuick_sort1.pptx
Quick_sort1.pptx
 
Coordinate Geometry
Coordinate GeometryCoordinate Geometry
Coordinate Geometry
 

Recently uploaded

Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersMairaAshraf6
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilVinayVitekari
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 

Recently uploaded (20)

Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 

Merge sort implementation

  • 2. Introduction to Group members Nazmul Hasan Rupu bsse 1034 Sadikul Haque Sadi bsse 1003
  • 3. Merge Sort  In merge sort algorithm , we use the concept of divide and conquer.  We divide the array into two parts ,then sort them and merge them to get sorted array.  This sorting method is an implementation of recursive function.
  • 4. Merge Sort  We take an array and keep dividing it from middle till we get only one element in each part(sub-array) .  Then we sort the sub-arrays and merge them to get the final sorted array.
  • 5. Example Let’s consider an array of 6 elements. 5 3 2 1 4 6 Arrange them in ascending order using merge sort algorithm.
  • 6. Array index Array elements 0 1 2 3 4 5 5 3 2 1 4 6 Begin Starting index End Last index Is Size >1 ? yes Divide the array into 2 part Mid = Size/2 Mid = 6/2 = 3 Size= 6
  • 7. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 5 3 2 We will consider the left sub-array
  • 8. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 5 3 2 Is Size > 1 ? Size=3 yes Divide the array into 2 part Mid = Size/2 Mid = 3/2 = 1
  • 9. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 5 3 2 0 5 Is Size > 1 ? no Size=1 As there is only one element in the array we now go to the right sub array
  • 10. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 5 3 2 0 5 0 1 3 2 Size =2 Is Size > 1 ? yes Divide the array into 2 part Mid = Size/2 Mid = 2/2 = 1
  • 11. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 5 3 2 0 5 0 1 3 2 0 3 1 2 As there is only one element in the array we now go to the right sub array
  • 12. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 5 3 2 0 5 0 1 3 2 0 3 1 2 Both the right sub- array and left sub- array have one element So we can now sort them and merge them
  • 13. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 5 3 2 0 5 0 1 3 2 0 3 1 2 0 1 2 2 0 1 2 3
  • 14. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 5 3 2 0 5 0 1 2 3 0 1 2 2 3 2 0 1 2 2 3 2 0 1 2 2 3 5
  • 15. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 2 3 5 The left sub-array is sorted now we will go to right sub-array 0 1 2 1 4 6 Now we will follow the same way as we followed so far
  • 16. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 2 3 5 0 1 2 1 4 6 0 1 0 1 4 6 0 4 0 6
  • 17. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 2 3 5 0 1 2 1 4 6 0 1 0 1 4 6 0 4 0 6
  • 18. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 2 3 5 0 1 2 1 4 6 0 1 0 1 4 6
  • 19. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 2 3 5 0 1 2 1 4 6 The left and right array are sortedWe can now merge them to get sorted array in ascending order
  • 20. 0 1 2 3 4 5 5 3 2 1 4 6 0 1 2 2 3 5 0 1 2 1 4 6
  • 21. 0 1 2 3 4 5 1 3 2 1 4 6 0 1 2 2 3 5 0 1 2 1 4 6
  • 22. 0 1 2 3 4 5 1 2 2 1 4 6 0 1 2 2 3 5 0 1 2 1 4 6
  • 23. 0 1 2 3 4 5 1 2 3 1 4 6 0 1 2 2 3 5 0 1 2 1 4 6
  • 24. 0 1 2 3 4 5 1 2 3 4 4 6 0 1 2 2 3 5 0 1 2 1 4 6
  • 25. 0 1 2 3 4 5 1 2 3 4 5 6 0 1 2 2 3 5 0 1 2 1 4 6 0 1 2 3 4 5 1 2 3 4 5 6 The array is now sorted ! ! !