 Welcome back. In the previous segment, we discussed the outline of merge sort and we also discussed the code for it. In this segment, we are going to discuss the merge function. So let us do an example first. So let us take the U sequence as 23295087. So this is the result of sorting that U that we had got by splitting our original sequence and V is the other part again sorted. So how do we merge this? So we have to produce the sequence S and if you think about it, what do we want in S? Well, the first element should be the overall smallest. Now how do we get the overall smallest? We need to look at both these sequences in great detail. Well no, because they are sorted, it is the smaller of the smallest which is present in the first position and the smallest over here which is also present in the first position. So if I want the smallest in these two sequences, I just need to look at these two positions. So if I look at these two positions, then I know that the smaller is 7. So that 7 must come down over here. So what we get after this is this picture. So we started over here, we looked at the two elements which are respectively smaller in U and V and we do not have to look at the rest of the elements to decide which is the smallest in this entire set. So we pick this 7 which is the smallest over here and we move it to S. And this we are going to continue. So what do we do next? So this is our situation, we again want the smallest of these two. So we really want the second smallest in what was originally U and V. But now that S has moved out we can say look what is the current situation of U and V and we want the smallest amongst those. So how do we get that? So the second smallest is the smallest in UV after the smallest has moved out. So which is exactly this position over here and to get that we simply have to ask what is the smaller among what is at the front of U and V. So the front of U is this. So here there is 23, the front of V is now this because this element has moved out. So we have to now pick the smaller of these two elements and move that at the end of S. So if we do that we get this. So the smaller has moved to the end of S or the back of S and the fronts, the elements which were originally at the fronts have gone away and the fronts have sort of advanced. So what is the general strategy? So while both U and V contain a number move the smallest from those at the front of UV to the end of S. If U contains only U contains numbers then that means everything in V has already been moved to S and so therefore move everything in U to the end of S similarly for V. So basically elements are leaving the arrays UV from the front and joining the array S at the back. Now you have encountered this. This is really what was happening in this taxi dispatch problem. So the code is also going to be somewhat similar. So what are the details? So we are going to maintain a variable UF which is going to denote the element of U, the index at which the front is currently. Similarly, so what that means is that elements 0 through UF minus 1 have already moved out and therefore the front has now advanced to index UF. Similarly there is VF and then we also want to have an index SV to the back of S. So this is the index denoting where the next element should move into S. So again 0 to SB minus 1 contain elements that have already moved in earlier. So this is really pretty similar to the taxi dispatch as you might be noting. So we also need to keep track of the lengths, we need to know the lengths of U and V and so let us say we use the variables LU and LV to denote the lengths. We could have a variable to denote the length of S but we know that S is going to be exactly equal to the lengths of LU and LV because we created U and V in that manner. So we need not define a variable for it separately. So that is basically it, we can get started on our function for doing the merging. So again let me explain, U is the first sequence that we want to merge, its length is LU, V is the second, its length is LV and S is the array into which the result is supposed to go and UF and VF are the positions of the fronts. So initially the front is at the 0th index, here and the front of V is also at the 0th index. We need an array for an element for the back but that will be a part of our main loop. So we are going to move elements at the back and so the back is going to keep on advancing. It is going to be 0, it will become, it will keep on increasing and when we have moved LU plus LV elements then we are going to stop. So that is how the whole overall structure is going to be. So how do we do this movement? Well if both U and V are non-empty and when will they be non-empty, well if UF is smaller than LU, so the front is still pointing to a valid element in U and if the front is pointing to a valid element in V then that means both U and V are non-empty. In which case what should we do? We should check which one is smaller. If the U side has the smaller element then the V side what should we do? Well we should move the U side element to the back of SB and we should advance the pointer, the front of UF. We should advance the back of UF as well but that will get advanced at the end of the loop anyway because of this statement over here. So we are not going to do that explicitly here. If on the other hand this V had the smaller element then we should do the same thing but with V rather than with U. So the front element of V is going to move to the back of S and the front for V is going to advance. So if one of those U and V is empty, so let us say U is not empty, so UF is smaller than U. So V is now empty. So in that case we do not have to do any comparisons, we just move whatever is at front of U to the back and then we just advance the front for U. Otherwise it means that only V is non-empty, so then we are going to do the same thing. Whatever is at front of V is going to be moved to the back of S and we are going to advance the front of V. So that is it. So this brace closes this brace and this brace is closing this brace over here. I do not want to put it later on because I just want to keep it, otherwise the font becomes too small. But anyway this is the code for merging these two sequences. You can check, so we have already discussed what exactly happens and so now we can analyze the time it takes. So how much time does this take? Well, it has this main loop. So this is the main loop and how many iterations is the main loop going to run? It is going to run LU plus LV iterations and therefore its time is going to be proportional to LU plus LV since in each iteration we are going to do some fixed amount of work. The work may be different depending upon whether both queues are non-empty or only one is non-empty but it is going to be some fixed amount of work. So if we are merging two sequences of length LU LV, the loop runs for LU plus LV iterations. We do a fixed amount of work in each iteration and so the time is proportional to LU plus LV. So I could say that the time, I can say that the time is proportional to N if LU plus LV is equal to N or if the final sequence has length N then the time is proportional to LU plus LV. So now we have decided what the time taken for merging is. So what have we discussed in this segment? We discussed how to merge sorted sequences and we have discussed that the time taken to merge two sequences of total length is proportional to N. In the next segment we are going to put together everything and we are going to do the analysis of the entire merge sort and we will also have a demo of the merge sort and then we will also conclude for this entire lecture sequence. So we will take a quick break.