 Hello friends, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to explain how to write an algorithm to match two sorted additives into third additives and that third additives will be automatically arranged in part. So first, let me draw two additives. Let's say A1, A2. So it is having four additives, 1, 2, 3, 4. Second additives, A2. It is having 2, 4, 6, 8. And we need to create third additives and it will be having all eight additives. So A1, A2 both are already in sorted form and we need to write algorithm in such a way so that if we merge both the additives, third additives will be automatically sorted. So now I am going to write the algorithm for you. So name of algorithm is merged. It will be having three parameters A1, A2, A3 as 1D arrays and two parameters as N1, N2. So N1 and N2 are size of first additives and second additives. So N1 is the size of A1, N2 is the size of A2. Now I am going to write step number one. So here I am writing set I equals to 1, J equals to 1 and K equals to 1. So I will be for A1, J will be for A2 and K will be for A3 to maintain the position of one. So as you know in algorithm the first element index is considered as 1. So that's why all the variables are initialized at 1. Now in step number two I am going to write three steps. So here one loop will be implemented and that will be repeating step number three and four, one. Right, so three and four steps will be repeated while I is equal to N1 and J is equal to N2. So if both I and J is equal to N1 and J is equal to N2 then only this loop will be repeated. Now step number three. So here we need to write F condition if A1 or 5. So we are going to compare A1 or 5 with A2 of J. So if A1 or 5 is less than A2 of J then we need to perform this operation. So here more than one operation will be there. So that's why I am writing AB. So set AB of K. So we are going to assign value to AB added on K position and that will be A1 or 5. So the value which is lesser will be assigned to AB because we want this card added should be in sorted form. And in these steps you can write set I equals to I person. So I am implementing value of I so that it will be positioning the next element of A1. Now if this condition is false, if this condition is false it means A1 or 5 is not less than A2 of J. So in L's part we can write set AB of K equals to A2 of J. And here in B step you can write set J equals to J person. So if first part is executed then I will be implemented. If second part is executed then J will be implemented. And whatever will be the case in both the cases after having value assigned on this position AB of A we need to increase K as well. So K equals to K plus 1. So this is the common step. So if condition is true then also K will be implemented by 1. If condition is false then also K will be implemented by 1. And here I am writing end of loop. So this loop will be implemented. So now let's iterate this part of algorithm. So this is not complete right now. So I am applying this on these arrays so that we can see what happens. So end 1 and 2. So let's say end 1 is 4. End 2 is also 4. Then we have I1 for this we have J as 1 for this we have A1. So check this condition both are true right now. So AO5 is less than A2 of J. So here 1 and here 2. So condition is true. So A1 position value will be assigned into A3. So 1 will shift here then I will be implemented so I is true then K will be implemented so K will be true. But J will remain 1. Again check these conditions. So still they are true. A1 of I so A1 of I is 2 now is less than A2 of J so false. So this time A2 of J will be shifted to A3 of K. So this 2 will be shifted here on 2 index and J will be implemented then K will be implemented. So K is on 3 position, J is on 2 position and I is also on 2 position. Then again check these conditions both are true. Then A1 of I is 2. A2 of J is 4. So again you can see condition is true. So 2 will be shifted here I will be implemented and K will be implemented. Then again check these conditions. So both are true. A1 of I, 3 position is happening 3. A2 of J, J is 2 so 2 position is 4. So again this condition is true. So from here 3 will be shifted here and I will be 4 and K will be 5. So again right now these 2 conditions are true. Check these A1 of I so A1 of 4. So 4 is having 4 and A2 of J is also having 4. So this condition is false now. So this time A2 when 4 will be shifted here J is 3 and K is 6. Again these conditions are true. A1 of I that is 4. A2 of J it is now 6. So this time this condition is true. So 4 will be shifted here and I will be implemented to 5. K will be implemented to 7. Now these 2 conditions are not true because I is 5 which is greater than N1. So this condition is false. So that's why this 2 will be done. And you can see 1, 2, 3, 4 from this area are copied. 2 or 4 are copied but 6 and 8 are immediately added as it is. So now what we need to do? We need to check whether J is equal to N2 or I is equal to N1. So this may be a scenario that we have some values available in first area as well. So second area is copied completely but first area is having some values as level. So now after completion of this rule, we need to apply 2 structure 2 so that we can copy the remaining element of a particular area. So for this I am writing step number 5 repeat steps. So it will be repeating the steps. So now step number 6 and 7 will be repeated while. So here I need to look into the condition. So while I less than equals to N1. So if I is still less than equals to N1 then this rule will repeat. So this time you can see I is 5 and N1 is 4. So this will be false. So this 2 will not repeat in this case. But in any other case that wherever A1 will be having remaining values then this rule will repeat. So here what we need to do? In step number 6 we need to copy A3 of A equals to A1 of I. So the remaining values of A1 of I will be copied to A3 of I and then in step number 7 I set I equals to I plus 1 and A equals to A plus 1 and then we can write N of A. So both I and A will be implemented and the remaining values of A1 will be copied to A2. So this is for A1. Now same steps we need to write for A2 as well. So that is the case right now. So all the values remaining that is section 8 will be copied as this here with that rule. So now I am going to write that rule. So step number 8 will be repeat steps 9 and 10 while J less than equals to N2. So this time J is on this position. So J is less than equals to N2. So we need to repeat step number 9. So here we need to write step A3 of A equals to A2 of J. So the values of A2 and it will be copied to A3. Then set A equals to A plus 1 and J equals to J plus 1 right. And then you can write N of A. So this rule will be calculated and in step number 11 you can write N so that your algorithm will be connected. So this rule will copy remaining elements of A1 if they are. This rule will copy remaining elements of A2 into A3 if they are. So in every time whenever you will be executing this algorithm either A2 will be having remaining elements or A1 will be having remaining elements. There will not be any case like both the areas A1 and A2 are having remaining elements. So this will not be a scenario. So maybe this will execute or maybe this will execute. So this way I will explain you how you can write algorithm so that we can merge two sorted areas into third area and here you can see third area in sorted form. So if we copy these elements as well with this rule. So as this is already in sorted form so those two sorted values will be available here. So you can see this third area is completely sorted in order. So this way I hope you understood how we can merge two sorted areas into third area back to in sorted form. And if you want to watch it's this algorithm implementation with C language. So in the description you can find link of that playlist and at the end of this we will find links so that you can learn how we can implement margin using C language. So thank you for watching this video.