SlideShare a Scribd company logo
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 transversals
masljr
 
Lesson 1 student notes
Lesson 1 student notesLesson 1 student notes
Lesson 1 student notes
Joanne Rosa Crooks
 
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
citlaly9
 
Rational Expressions
Rational ExpressionsRational Expressions
Rational Expressions
Lorie Jane Letada
 
Content schedule bt
Content schedule btContent schedule bt
Content schedule bt
btmathematics
 
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
detwilerr
 
Python week4-lecture1-handout
Python week4-lecture1-handoutPython week4-lecture1-handout
Python week4-lecture1-handout
AnmolTwickley
 
Parallel Lines Angles
Parallel Lines AnglesParallel Lines Angles
Parallel Lines Angles
Dyan Aniol
 
Math chapter 3 multiplying exponential expressions
Math chapter 3 multiplying exponential expressionsMath chapter 3 multiplying exponential expressions
Math chapter 3 multiplying exponential expressions
JaredSalvan
 
Math chapter 3 dividing exponential expressions
Math chapter 3 dividing exponential expressionsMath chapter 3 dividing exponential expressions
Math chapter 3 dividing exponential expressions
JaredSalvan
 
Understanding exponents
Understanding exponentsUnderstanding exponents
Understanding exponents
Michelle Barnhill
 
3rd Test - Slope and parallel lines
3rd Test - Slope and parallel lines3rd Test - Slope and parallel lines
3rd Test - Slope and parallel lines
Brandeis High School
 
L 50
L 50L 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
REYBETH RACELIS
 
Unit 6 review
Unit 6 reviewUnit 6 review
Unit 6 review
mlabuski
 
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 expressions
holmsted
 
Mean, Median And Mode
Mean, Median And ModeMean, Median And Mode
Mean, Median And Mode
stanhopekris
 
Adding similar fractions
Adding similar fractionsAdding similar fractions
Adding similar fractions
jennytuazon01630
 
Exponents Rules
Exponents RulesExponents Rules
Exponents Rules
BigPassy2
 

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.pdf
ArjunSingh81957
 
Sorting
SortingSorting
Sorting
Jasmine Chen
 
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
Motaleb Hossen Manik
 
Quick sort-170316064200
Quick sort-170316064200Quick sort-170316064200
Quick sort-170316064200
ifeanyiokeke3
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
Jeanie 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.pptx
chacha987301
 
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
Abdii Rashid
 
Sorting
SortingSorting
Lattice multiplication
Lattice multiplicationLattice multiplication
Lattice multiplication
Batangas National High School
 
Algo PPT.pdf
Algo PPT.pdfAlgo PPT.pdf
Algo PPT.pdf
sheraz7288
 
Section 8: Symmetric Groups
Section 8: Symmetric GroupsSection 8: Symmetric Groups
Section 8: Symmetric Groups
Kevin Johnson
 
Merge sort
Merge sortMerge sort
Merge sort
Marci McClenon
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
MuhammadSheraz836877
 
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 Geometry
hiratufail
 
Aizaz comb sort
Aizaz comb sortAizaz comb sort
Aizaz comb sort
Nauman Ali
 
Quick_sort1.pptx
Quick_sort1.pptxQuick_sort1.pptx
Quick_sort1.pptx
sandeep54552
 
Coordinate Geometry
Coordinate GeometryCoordinate Geometry
Coordinate Geometry
Manisha 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

ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
Mukeshwaran Balu
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
yokeleetan1
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
Madhumitha Jayaram
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
PauloRodrigues104553
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
nooriasukmaningtyas
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 

Recently uploaded (20)

ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 

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 ! ! !