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