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 (20)

PPT
3.2 use parallel lines and transversals
masljr
 
PDF
Lesson 1 student notes
Joanne Rosa Crooks
 
PPTX
Adding and subtracting positive and negative rational number notes 1
citlaly9
 
DOCX
Rational Expressions
Lorie Jane Letada
 
PDF
Content schedule bt
btmathematics
 
PPTX
3.2 use parallel lines and transversals
detwilerr
 
PDF
Python week4-lecture1-handout
AnmolTwickley
 
PPTX
Parallel Lines Angles
Dyan Aniol
 
PPTX
Math chapter 3 multiplying exponential expressions
JaredSalvan
 
PPTX
Math chapter 3 dividing exponential expressions
JaredSalvan
 
PPT
Understanding exponents
Michelle Barnhill
 
PPT
3rd Test - Slope and parallel lines
Brandeis High School
 
PDF
L 50
andreagoings
 
PDF
Rationalizing the Denominator of a Radical Expression
REYBETH RACELIS
 
DOC
Unit 6 review
mlabuski
 
PDF
Unit 1 BP801T a introduction mean median mode
ashish7sattee
 
PPT
Roots and radical expressions
holmsted
 
PPT
Mean, Median And Mode
stanhopekris
 
PDF
Adding similar fractions
jennytuazon01630
 
PPTX
Exponents Rules
BigPassy2
 
3.2 use parallel lines and transversals
masljr
 
Lesson 1 student notes
Joanne Rosa Crooks
 
Adding and subtracting positive and negative rational number notes 1
citlaly9
 
Rational Expressions
Lorie Jane Letada
 
Content schedule bt
btmathematics
 
3.2 use parallel lines and transversals
detwilerr
 
Python week4-lecture1-handout
AnmolTwickley
 
Parallel Lines Angles
Dyan Aniol
 
Math chapter 3 multiplying exponential expressions
JaredSalvan
 
Math chapter 3 dividing exponential expressions
JaredSalvan
 
Understanding exponents
Michelle Barnhill
 
3rd Test - Slope and parallel lines
Brandeis High School
 
Rationalizing the Denominator of a Radical Expression
REYBETH RACELIS
 
Unit 6 review
mlabuski
 
Unit 1 BP801T a introduction mean median mode
ashish7sattee
 
Roots and radical expressions
holmsted
 
Mean, Median And Mode
stanhopekris
 
Adding similar fractions
jennytuazon01630
 
Exponents Rules
BigPassy2
 

Similar to Merge sort implementation (20)

PDF
Working of Merge Sort Code
Muhammad Abdullah
 
PPTX
Sorting in data structures and algorithms , it has all the necessary points t...
BhumikaBiyani1
 
PPTX
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
PPTX
Merge sort
Sindhoo Oad
 
PPTX
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
PPTX
dsa presentation on merge sorting in C++.pptx
uzairahmedua519020
 
PPTX
Merge sort
Rojin Khadka
 
PDF
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
ArjunSingh81957
 
PPTX
Mergesort
SimoniShah6
 
PPTX
Data Structure Marge sort Group 5 pptx so
Salma368452
 
PPTX
Merge sort
lakshitha perera
 
PPTX
MergeSort presentation dgdfgdfgdfgg.pptx
palmakyonna
 
PPTX
sorting-160810203705.pptx
VarchasvaTiwari2
 
PPTX
Daa final
Gagan019
 
PPTX
Merge sort analysis and its real time applications
yazad dumasia
 
PPTX
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
PDF
Sorting Algorithms and their implementations
ChakravarthiMusic1
 
PPTX
Data Structures and Agorithm: DS 20 Merge Sort.pptx
RashidFaridChishti
 
Working of Merge Sort Code
Muhammad Abdullah
 
Sorting in data structures and algorithms , it has all the necessary points t...
BhumikaBiyani1
 
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
Merge sort
Sindhoo Oad
 
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
dsa presentation on merge sorting in C++.pptx
uzairahmedua519020
 
Merge sort
Rojin Khadka
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
ArjunSingh81957
 
Mergesort
SimoniShah6
 
Data Structure Marge sort Group 5 pptx so
Salma368452
 
Merge sort
lakshitha perera
 
MergeSort presentation dgdfgdfgdfgg.pptx
palmakyonna
 
sorting-160810203705.pptx
VarchasvaTiwari2
 
Daa final
Gagan019
 
Merge sort analysis and its real time applications
yazad dumasia
 
SORT AND SEARCH ARRAY WITH WITH C++.pptx
narifmsit18seecs
 
Sorting Algorithms and their implementations
ChakravarthiMusic1
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
RashidFaridChishti
 
Ad

Recently uploaded (20)

PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
Big Data and Data Science hype .pptx
SUNEEL37
 
PPTX
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PDF
smart lot access control system with eye
rasabzahra
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Big Data and Data Science hype .pptx
SUNEEL37
 
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
smart lot access control system with eye
rasabzahra
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Ad

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