 Hello and welcome to this video lecture. This is the fourth video in this series of video lectures on sorting. Previously we have covered videos on the topics of bubble sort, insertion sort and the previous video was on selection sort. In today's video we are going to see the merge sort technique. So after the end of this session you will understand the working of merge sort and how to use merge sorting technique to sort given set of integers. We are going to see the working of merge sort. Before we proceed with learning merge sort I want to strongly encourage you to go through these concepts in C so that you will understand merge sort easily. The videos on these concepts are available on the WIT PLC channel. You can watch those videos and be well versed with these concepts before you go ahead with learning sorting. So talking about merge sort merge sort follows divide and conquer approach. Now what is this divide and conquer approach? Now the size of the array to be sorted is n. So what merge sort does is it divides the arrays into smaller sub arrays and the smaller sub arrays are recursively or repeatedly divided into smaller sub sub arrays. When the sub arrays or as we say sub sub arrays are of size one that is n equals one these are compared with each other and swapped if required. And once the sub arrays or the sub sub arrays are of size one and they are swapped they are again merged when compared with one another to form the final sorted array. This picture will give us a clear understanding of the working of merge sort. Say for example we have n elements where n is 8 in this example where the array holds n elements or 8 elements in a random order. So q is nothing but an integer or a value which divides the array into two parts. Similarly p and r are the indices of the sub array and q again is the value which divides the sub array into two equal sub sub arrays. So we have a left subtree and a right subtree so to say in a pictorial format. And this division takes place till we have a sub sub array of size one where each element in the array is divided and compared with the element to find the correct position in the sorted array. So firstly all the elements in the left sub array are recursively divided and then this array or the sub array is formed. Next all the elements in the right sub array are divided till single elements are available and then they are merged in their correct order. Similarly we have two sub arrays which are sorted. If you see 3, 7, 12, 14 is a sub array of the original array but these four elements are in its sorted order. Similarly 2, 6, 9, 11 is a sorted sub array of the original array. And lastly we see when we combine these two sub arrays we come to our solution that is a sorted array of size n that is 8. This is how merge sort works to sort in elements using divide and conquer approach. We shall see a simulation of merge sort. This is hacker earth.com platform and on this platform I am going to see the simulation and I am going to show you how merge sort actually works. So let us take 8 random valued and array of 8 random values and check for the working of merge sort. So firstly we see that the array is split into two then the left sub array is again split into two then we see that the left most sub sub array is split into two. So we see that now 5 and 8 are put in its correct order that is 5 in the first position and 8 in the second position. Now we come to the next sub array of the first sub array that is 4 and 1. So 4 and 1 have been divided so that they are now of size 1. The sub array is of size 1. Now these will be compared and 1 will take its correct position followed by 4. Now 5, 8, 1, 4, 5 will be compared, this element is compared with the first element and it finds the correct position where 1 comes first before 5 followed by 5. So we see that both the sub arrays are compared to check for its correct values. So since 4 was in the second sub array but its value is less than 5 so the complete sub array is filled with sorted values. So it is filled with values 1, 4, 5, 8 where all these 4 values 1, 4, 5, 8 are from the left sub array but are in the sorted order. Now the control goes to the right sub array. So the left most values that is 2 and 6 are now split up till they form a sub array of size 1. Since they are in the correct order the sub array will be retained as 2 and 6. Only 3 and 9 will also be retained because they are in the correct order. Now both these sub arrays will be compared and they will be sorted in their correct order. So once the comparisons between the sub array takes place 2 is followed by the value 3 followed by the value 6 and the last value to be followed is 9. So now we want to put all these 8 values by comparing them with one another into the original array. So when we do the comparison we will see that number 1 comes into the first position followed by 2 from the right sub array followed by 3 again from the right sub array followed by 4 from the first sub array followed by 5 from the left sub array followed by 6 from the right sub array followed by 8 from the left sub array and followed by 9 from the right sub array. So this is how the merging is over and merge sort finishes by merging the divided sub arrays back into the array by putting the elements in the sorted order after comparison. So we are done with sorting and you can see that the array is in its final sorted position. So this is the algorithm we find the middle point of the array to divide the array into two halves by finding q where the value of q is the value of p plus r divided by 2. So whatever the is the value of p and r we divided by 2 and then find the value of q then we recursively call merge sort for the first half when we find and implement merge sort for the first half and it gives us a sub array which is sorted then we call merge array merge sort for the second half that is for the second sub array and then we merge the two halves sorted which are sorted already sorted in step number 2 and 3 we merge them using the merge function where we pass the complete array and the three indices. So here I want you to stop the video and answer the following question we have discussed a methodology to which this sorting algorithm belongs to. So I want you to answer this question that merge sort follows which of the following algorithm design methodology pause the video and answer the question. So since merge sort initially divides the array into sub arrays and it combines the sub arrays to form the completed sorted array we come to know that it follows divide and conquer methodology. These are the references. Thank you very much.