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

Merge sort implementation

  • 1.
  • 2.
    Introduction to Groupmembers Nazmul Hasan Rupu bsse 1034 Sadikul Haque Sadi bsse 1003
  • 3.
    Merge Sort  Inmerge 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  Wetake 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 anarray of 6 elements. 5 3 2 1 4 6 Arrange them in ascending order using merge sort algorithm.
  • 6.
    Array index Array elements 01 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 23 4 5 5 3 2 1 4 6 0 1 2 5 3 2 We will consider the left sub-array
  • 8.
    0 1 23 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 23 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 23 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 23 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 23 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 23 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 23 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 23 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 23 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 23 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 23 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 23 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 23 4 5 5 3 2 1 4 6 0 1 2 2 3 5 0 1 2 1 4 6
  • 21.
    0 1 23 4 5 1 3 2 1 4 6 0 1 2 2 3 5 0 1 2 1 4 6
  • 22.
    0 1 23 4 5 1 2 2 1 4 6 0 1 2 2 3 5 0 1 2 1 4 6
  • 23.
    0 1 23 4 5 1 2 3 1 4 6 0 1 2 2 3 5 0 1 2 1 4 6
  • 24.
    0 1 23 4 5 1 2 3 4 4 6 0 1 2 2 3 5 0 1 2 1 4 6
  • 25.
    0 1 23 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 ! ! !