In the previous segments, if you notice carefully, you’ll see that we did two different operations. First, we divided the sequence up to single elements. Second, we merged them back in the required order. In the next video, you’ll see how you can do this in Java.
You can download the Java file for Merge Sort code being used in the video below.
Now let us take a look at a simple implementation of the merge sort. So for here, let us first define a merge function which will take in an input array, the starting index of that input array, the middle element, the mid index of that input array and the ending index. So now what I'll do is first I'll calculate the length of my left subarray. So what will be my length of the left subarray? It will be the mid element minus the start index plus one. Now I'll calculate the length of the right subarray. So what will be the length of the right subarray? It will be n minus mid, that is n minus m. Now I'll declare two new subarrays l and r, which will store my left and right subarrays. After declaring them, I'll simply populate these two arrays. That is, I starting from zero till the left length I'll populate li equal to array of start plus i. Moreover, for populating my right subarray, I'll start my iterated j from zero going till the length of my right subarray. And I'll say r of j is equal to array of m plus j plus one. Now here I'll declare two new variables int I equals to zero and j equals to zero. And another variable k, whose initial value will be start. Now, in the coming procedure, we are going to do the actual merging process. So here I am saying that while I is less than the length of the left subarray and j is less than the length of the right subarray, if my l of I is less than equal to r of j, then I copy the value of the lesser element. That is, I copy the value of l of I into array of k and increment the I pointer. If the opposite is true, that is, if l of I is greater than r of j, then this means that r of j is a smaller value. And so I copy it into my array of k and increment the j pointer. Now after this, if else is over, I anyway have to increment my k pointer. So this is what we are doing here. Now, as we discussed in the pseudocode, there might arise a situation when my right array has exhausted and I'm only left with elements in the left subarray. In that case, what I'll do is that while my I is less than the length of the left subarray, I'll copy all such li elements into array of k. Then I will increment my I and k pointer.
Similarly, as we discussed in the pseudocode, if there arises a situation such that my left subarray has been exhausted and I am only left with my right subarray, in that case, I would sequentially copy all of my RJ values into array of k and increment the j and the k pointer. Now, let us look at this particular sorting function. What this sorting function does is that it takes in an input array, the starting index of that input array and the ending index. Now I say that if start is less than end then first I calculate the mid index which will be equal to start plus end by two. Then I'll call the same sort function on the smaller subarray that is sort of array comma start comma the mid. Similarly, I'll call the sort function on the remaining right subarray. So that will be sort of array comma m plus one comma end. And finally I will call my merge function taking in input parameters as array start m and end. Note that this merge function is the same which we defined here. Now let us try to run this code and see if it works correctly. For that we have taken an input array like this. This is clearly unsorted at this stage. Now we'll run and check. So here you can see that this array is finally getting printed in its correct sorted order. So this is how merge sort works.
Merge sort is a sorting algorithm that utilizes the concept of divide and conquer.
A merge function is defined which takes in an input array, the starting index of that input array, the middle element, the mid index of that input array, and the ending index.
The length of the left and right subarrays are calculated.
Two new subarrays, i and r, are declared and populated with the respective elements of the left and right subarrays.
A merging process is executed where the smaller of the two elements is copied into a new array and the respective pointer is incremented.
If one of the subarrays has been exhausted, then the remaining elements of the other subarray are copied into the new array.
A sorting function is defined that takes in an input array, the starting index, and the ending index.
The sorting function calculates the mid index and calls itself recursively on the smaller subarrays.
Finally, the merge function is called on the left and right subarrays to sort and merge them.
The code is tested and verified to work correctly.
Note that the above code discussed by the Aishwarya is just one version of merge sort code being used. Obviously, it can have other versions as well. One of those can be found here. Please do visit this link. It will help you broaden your understanding of the algorithm.
Now, it's time to analyse this algorithm. We do it in the next segment.