 Welcome back. In the previous segment, we saw binary search. In this segment, we will see a nice sorting algorithm. So what is the sorting problem? Well, the input is an array containing numbers and the goal is to rearrange numbers in non-decreasing order. We have already seen one algorithm for it, the so-called selection sort algorithm. And we said there was a time taken is going to be about n squared or rather proportional to n squared. Now we are going to see an algorithm called merge sort whose time is going to be proportional to n log n. So this is going to be considerably faster than selection sort. So let me first describe the main idea of merge sort. So suppose we are given a long sequence or an array, a long array which holds that long sequence. So we are going to take that sequence and break it into small sequences. Then we are going to sort each small sequence independently separately and this is going to happen using recursion. Then we are somehow going to merge the sorted sequences into a single long sequence. The hope is that merging the sorted sequences is easier than sorting the large sequence in the first place. And yes, this works out as we will see soon. So let us take an example just to make sure that this structure is understood. So say we want to sort the sequence. So the sequence has 7 elements and let us say the sequence is sitting in an array of size 7. But really let us think about it as sequences rather than arrays for the minute. So if you want to sort it, our first step was to break it into 2 sequences. So let us say the first 4 elements form the first sequence and the last 3 elements form the second sequence. So we are going to sort both. So when we sort both, we are going to get this sequence over here. So this is the sorted version of this and this is the sorted version of this. So these are the 2 sequences we now have. After we have recursively sorted the smaller sequences. Then we are going to merge them. So in the merge from this input, these sequence and this sequence given to us, we want to produce this output. So we want to have the same numbers but they should be in increasing order or non-decreasing order really. So that is sort of the overall structure of what we are attempting to do. So that overall structure can already be written into a program and so let us do that. So merge sort is our main function and this is going to sort sequences, a sequence s of length n. So that sequence is an argument. So there is a natural base case. If n is equal to 1 then there is nothing to do. We just have to return. The s itself is already sorted. If n is bigger than 1 then we said that we are going to break it up into 2 small sequences. So let us call those 2 sequences u and v. So we want, we are going to break it nearly into halves. So this is going to be n by 2 and this is going to be n minus n by 2. Notice that I am not writing n by 2 over here. That is just to guard against the possibility of n being odd and things like that. If n is odd then that will not work. So what I am doing is this plus this and just this is making sure that this plus this is equal to n. So that way I know that all the elements will either come over here or come over here. Then in this step I am going to make a copy of the first half into u and in this step I am going to make a copy of the second half into v. So this starts off where this ended and so I am going to get a copy of the second half. So I have created now my 2 small sequences. What do I do next? I have to sort them. So how do I do that? Well, this is where recursion comes to my rescue and so I am going to just issue this call. Sort sequence u whose length is n by 2 and similarly I am going to sort sequence v whose length is n minus n by 2. At this point I have those 2 sequences sorted in u. So they went into merge sort in u and they are coming back in u and now I am going to merge these 2 sequences. So u and v are the 2 sequences that I had and I am going to get them merged into s. So that is it because s will have the final sorted sequence which is exactly what we wanted over here. The result had to be in s itself. So that is what we are going to have. Now I want to leave you with an exercise before I conclude this segment which is that we will discuss the merge function next but suppose it is somehow available. I would like you to show me how merge sort will sort the sequence 50, 20, the given sequence. Basically I want you to draw the recursion tree which we have learned earlier. So in the recursion tree show the sequence that is the argument to each call and the sequence returned by each call. So this will just be a test of whether you understand what the algorithm is doing at the high level. So what have we discussed? We have discussed the outline of merge sort and this consists of split the given sequence into 2 parts, sort the 2 parts recursively and merge the 2 sorted sequences. Then we also discuss the code for the merge sort function and what remains to discuss is the merge function. So that we are going to discuss in the next segment. So we will take a quick break.