Merge Sort is an efficient algorithm that works on Divide and Conquer strategy. It might look a little complex than Bubble Sort or Insertion Sort. But no worries, we will make it as simple as Bubble Sort or Insertion Sort.
As said, Merge Sort works on Divide and Conquer strategy. Just think Divide and Conquer strategy is to keep on dividing a list into approximately two equal halfs until there is only one element left in each divided list. And then comes the Conquer step, where we start combining the lists after sorting them.
Just remember that its a three step process :
Already looks complicated !! Let us simplify this with an example,
Let us say there are 4 elements in a list and we need to sort them in ascending order.
So, at first it is the DIVIDE step. And we go on dividing each part, until there is only one element left in the list.
Now, as said we will divide the above list 'k' into two equal parts. So, that each part contains 2 elements each.
Now, we have 2 lists l1 and l2. And we have to subdivide l1 & l2 again as, we have to keep on subdividing the lists until we have 1 element in each list.
Finally, we are done with the divide step. As each list m1, m2, m3 and m4 contains one element each.
Now, it is time to CONQUER.
Now, let us look at the entire diagram and see, how the list is divided.
In Conquer step we will start from the very left and sort individual lists.
The very left element is
And since there is only one element in the list, we can assume that the above list is already sorted.
Similarly,
Also contains just one element and we can assume the above list is also sorted.
Now, it's time to COMBINE/MERGE.
So, we need to Combine/Merge 7 and 4 back to the sublist from which it was divided(Off course after sorting them).
And, the immediate list from where 7 and 4 got subdivided is,
So, we check if the element in the m1 list is less than the element in the m2 list. And we find 7 in greater than 4. So, we swap the positions of 7 and 4 and put them back to the list l1.
Now, the new list becomes,
And we can discard the lists m1 and m2.
And, the list l1 is sorted.
So, we try going up and find the list k has one more sublist l2(Which is not yet sorted).
Now, let us take the sublist l2,
Now, it is the Conquer step where we intend to sort the lists m3 and m4,
Since, the lists m3 and m4 has only one elements each, it is considered to be sorted.
Now, we combine/merge them.
So, we check if the element in the m3 list is less than the element in the m4 list. And we find 8 in greater than 1. So, we swap the positions of 8 and 1 and put them back to the list l2.
Now, the new list becomes,
So, we can say that l1 and l2 is sorted.
Now, it's time we take a look at the main list k.
Since, l1 and l2 is sorted it's time to Combine/Merge l1 and l2 with the main list k.
Now, we need to Combine/Merge list l1 and l2 back to the main list k from which it was divided.
Now, comes the main part.
Now, since we have understood the way Merge Sort Algorithm works. Let us understand the bigger picture from coding perspective.
Here, we will go all the way down to the left, breaking the list and come up by merging it.Let's see how?
Here, we will go all the way down to the left, breaking the list and come up by merging it.Let's see how?