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

PPTX
Rational algebraic expressions
PPTX
Multiplying & dividing rational algebraic expressions
PDF
Algebra 2 Section 2-2
PDF
PPTX
6.3 solve by elimination
PPT
Algaebraic forms
PPTX
Engineering maths/Exact Differential equations
Rational algebraic expressions
Multiplying & dividing rational algebraic expressions
Algebra 2 Section 2-2
6.3 solve by elimination
Algaebraic forms
Engineering maths/Exact Differential equations

What's hot (20)

PPT
3.2 use parallel lines and transversals
PDF
Lesson 1 student notes
PPTX
Adding and subtracting positive and negative rational number notes 1
DOCX
Rational Expressions
PDF
Content schedule bt
PPTX
3.2 use parallel lines and transversals
PDF
Python week4-lecture1-handout
PPTX
Parallel Lines Angles
PPTX
Math chapter 3 multiplying exponential expressions
PPTX
Math chapter 3 dividing exponential expressions
PPT
Understanding exponents
PPT
3rd Test - Slope and parallel lines
PDF
PDF
Rationalizing the Denominator of a Radical Expression
DOC
Unit 6 review
PDF
Unit 1 BP801T a introduction mean median mode
PPT
Roots and radical expressions
PPT
Mean, Median And Mode
PDF
Adding similar fractions
PPTX
Exponents Rules
3.2 use parallel lines and transversals
Lesson 1 student notes
Adding and subtracting positive and negative rational number notes 1
Rational Expressions
Content schedule bt
3.2 use parallel lines and transversals
Python week4-lecture1-handout
Parallel Lines Angles
Math chapter 3 multiplying exponential expressions
Math chapter 3 dividing exponential expressions
Understanding exponents
3rd Test - Slope and parallel lines
Rationalizing the Denominator of a Radical Expression
Unit 6 review
Unit 1 BP801T a introduction mean median mode
Roots and radical expressions
Mean, Median And Mode
Adding similar fractions
Exponents Rules
Ad

Similar to Merge sort implementation (20)

PDF
Working of Merge Sort Code
PPTX
Sorting in data structures and algorithms , it has all the necessary points t...
PPTX
Weak 11-12 Sorting update.pptxbhjiiuuuuu
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPTX
Merge sort
PPTX
2.Problem Solving Techniques and Data Structures.pptx
PPTX
dsa presentation on merge sorting in C++.pptx
PPTX
Merge sort
PDF
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
PPTX
Mergesort
PPTX
Data Structure Marge sort Group 5 pptx so
PPTX
Merge sort
PPTX
MergeSort presentation dgdfgdfgdfgg.pptx
PPTX
sorting-160810203705.pptx
PPTX
Daa final
PPTX
Merge sort analysis and its real time applications
PPTX
SORT AND SEARCH ARRAY WITH WITH C++.pptx
PDF
Sorting Algorithms and their implementations
PPTX
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Working of Merge Sort Code
Sorting in data structures and algorithms , it has all the necessary points t...
Weak 11-12 Sorting update.pptxbhjiiuuuuu
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
Merge sort
2.Problem Solving Techniques and Data Structures.pptx
dsa presentation on merge sorting in C++.pptx
Merge sort
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Mergesort
Data Structure Marge sort Group 5 pptx so
Merge sort
MergeSort presentation dgdfgdfgdfgg.pptx
sorting-160810203705.pptx
Daa final
Merge sort analysis and its real time applications
SORT AND SEARCH ARRAY WITH WITH C++.pptx
Sorting Algorithms and their implementations
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Ad

Recently uploaded (20)

PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
Digital Logic Computer Design lecture notes
PPTX
Artificial Intelligence
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
CH1 Production IntroductoryConcepts.pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Construction Project Organization Group 2.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
web development for engineering and engineering
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Sustainable Sites - Green Building Construction
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
UNIT 4 Total Quality Management .pptx
Safety Seminar civil to be ensured for safe working.
Digital Logic Computer Design lecture notes
Artificial Intelligence
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
CH1 Production IntroductoryConcepts.pptx
Mechanical Engineering MATERIALS Selection
Construction Project Organization Group 2.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
web development for engineering and engineering
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Internet of Things (IOT) - A guide to understanding
Lecture Notes Electrical Wiring System Components
Sustainable Sites - Green Building Construction
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...

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