 So in the last class we were discussing about what to discuss, we discussed about the horizontal mark right, now vertical mark, vertical mark. Now today first we will be discussing horizontal mark and then we will discuss the main sorting algorithm and finally we will try to consider an example through which we can see how the algorithm works. So in the horizontal mark first the problem is that suppose you have j cos k, the problem is that this is increasing order, this is decreasing order and you want to mark it right. In order to do that we need to define a two column mark that problem is that you have one column j rows and two j elements, say you have a 0, a 1, a 2, a 2, simplicity writer a 1, a 2, a 2 j, these two j elements you have which is a mitotic sequence, the second man absent one is Omithal and the other one this is a 17 right, 4 plus 4, 8, 9, 13, 17. So a 1, a 2, a 2 j is a bitonic sequence and the elements are stored in a column of j rows in such a way that you have a 1, a 2, a j and then a j plus 1 to a 2 j and let us assume that this data is stored like this p 0, p 1, p j minus 1 right, this p 0, p 1, p j minus 1 are the processors and this is in RS register, this is in RT register or RR register, RR register. So you have a 1, a 2, a 2, j elements form a bitotic sequence and you have a column of j rows and each row is assigned to a processor p 0 to p j minus 1 and ith row contains the ai and ai plus j agreed, ai is in RS register and ai plus j is in RR register. So what you are going to do is see the simple thing is that in the bitonic sequence is based on the only one provided that ai is compared with ai plus s. So this is ready for you that this has to be compared, this has to be compared okay. So if you compare and rejected element will be in this side and accepted element will be this side right. Now after that I want to do it recursively right. So what happens here that after comparing this element will be, this element will be a bitonic sequence, this element will form a bitonic sequence and all these elements will be less than or equals to this one agreed or not? So and then recursively we will be solving these two. So same idea that you have this one compared exchange, now you have to bring these elements in this zone and these elements should go in this zone. If I then see it then this will become a bitonic sequence, this will become another bitonic sequence and all these elements will be smaller than these elements. Yes, no, any confusion and what I am doing this is compared with this, the rejected elements are lying here, accepted elements are lying here agreed. Now I want to make it these two halves in order to make these two halves and recursively I want to use it. So this half should be interchanged with this half. So so that this forms a bitonic sequence, this form another bitonic sequence and all these elements will be smaller than these elements and then you do it recursively. So for example there will be bitonic sequence of 16 elements 1, 2, 3, 4, 5, 7, 1. So this is a bitonic sequence you have, right and this is an r s register and this is an r r register. What we are doing that e i is to be compared with e i plus n and the accepted elements in r s area are r register. So if it is the case you get the result of this slide. Now you observe you observe that this becomes a bitonic sequence and this becomes another bitonic sequence. Now here is by the definition of the from the theorem of bitonic sequence. Now you want to use the same algorithm, you want to use the same algorithm for your, so what I want that these elements should be here. So then I get the bitonic sequence and these elements should be here so then I get another bitonic sequence. So what I will be doing I want to interchange this too. So if I interchange it I get 1, 2, 5, 8, 7, 6, 4, 3 and you get 15, 14, 12, 11, 9, 10, 13, 16. So you get you observe that you get 2 bitonic sequence and all these elements will be smaller than these elements. Now again you perform two hops, compare exchange and then interchange these two elements. So you get here 1, 1, 7, 2, 6, 4, 5, 3, 8, 9, 15, 10, 14, 12, 11, 13, 16 and if I interchange these two I get 1, 2, 4, 3, 7, 6, 5, 8, 9, 10, 12, 11, 15, 14, 13, 16. Now you observe that this is a cluster of four elements, this is a cluster of another four elements and each cluster is a bitonic sequence and again you do compare exchange 1. So compare exchange and then interchange so you will get then 5, 6, 7, 8, 9, 10, 12, 11, 13, 14, 15, 16. And then you get basically 8 such bitonic sequence you just do the compare exchange you get 1, 2, 3, 4, 5, 6, 7, 8 so you get the shorter sequence in this form. Here now if you observe that I could have done after doing the compare exchange you observe that this sequence is a bitonic sequence and all these elements should be smaller than this element. And now I could have done the column march, I could have done the column march and I could have got this sorted. Yes, this is the home of the, it is based on the part of the series but the problem is that I do not achieve the goal like that 1, 2, 3, 4, 5, 6, 7, 8 like that which I need for horizontal march because in the horizontal march this should be like this not this way. So that is the reason why instead of doing the column march we are doing the interchange first and then we are repeating the two column march so that the data is to final data is stored in this form. Is it okay? 2 column march first step is that perform compare exchange operation between RS and RR registers right. We have already assumed the data is in the form that A1, A2, A3, A2, J in such way that AI and AI plus J is in the IA through and RS and RR register respectively. Now if J is greater than 1, if J is equals to 1 that means there is only one row and that means that you have the only two elements which form the by today's sequence just one compare exchange is sufficient to make it sorted form. Now if J is greater than 1 what are you going to do? The rejected element has to be interchanged with excepted element of that. So interchange content of RR of PI, I is 0 to J by 2 minus 1 with RS of PK, K is J by 2 to J minus 1. Now how are you going to do this? See what we told this has to be interchanged. How are you going to do that? X equals to interchange what is X? How are you going to get these two values? This is in PI and this is in say PK and it is in the form of mesh. PI is connected to its four neighbor only. That is the reason why we need RD. So the elements here you move to RD then bring down next step you bring down all these elements here interchanges then you bring back. So this is the and then finally you move again this element. Now after interchanging you will be doing two column R J by 2 with J P 0, P J by 2 minus 1 and two column R J by 2, P J by 2 to P J minus 1 simultaneously. Both of them can be done in parallel. Now if it is the case then what is the time complexity? I do not think R to C and you have J. So if J is equals to 1 then it is 0 there is no routing. Now if J is greater than 1 obviously you have T R J by 2 to C plus what is the routing required? This data has to be moved here and then again you will be moving back. So what is the routing step total number? Can that not be done simultaneously? Which one? Bring this data down and take this data out. How? So for this half of this data you are bringing here and the left one take out. How are you going to because I told you the routing R R register is the only register through which. So we can store the above R R in RT. How? R R will send it to R R only, routing register also the initial value. This one you are storing here. R T. R T and similarly in the left above. Similarly this one you want to store here. This R S contains your original data. R S contains you cannot touch R S. R S these R S is all accepted data. This you want to retain here. This contains all rejected data but you want to keep them here. Only this part has to be interchanged with this part. In order to do that and your routing is done through R R register only. So you move this data here, you bring this data here, interchanged these two, then you send back the data here. Then this you bring back. Or reverse way, this you do one of the method. So you need R routing, R by 2 this side again R by 2 going back. You need R routaries, R this side, R is J, J routing. Solution of this, you assume J is 2 to the power. Tell me, what is the solution? J plus J by 2 plus J by 4. 2 J. 2 J or 2 J minus 2. 2 J minus 2. Last time we did it. I think it is 2 J minus 2. Next one is number of exchange operations. First let us do the number of comparative exchange. It is 1 if J is equals to 1. Otherwise it is 1 plus 2 C J by 2. Agreed? Because there is no comparative exchange operation. So I write 1 plus P C 2 C J by 2. Yes, and this solution is not given. This solution is log J. What about here? There is no exchange operation. So J or if J equals to 1, this side I am writing P E 2 C J by 2 if J greater than 1. How many exchange operations you are performing here? First is 1 is you are bringing data here. Then second is you are bringing data here, exchange operations. Then you are sending back through routing. Then again you will be bringing the data here. 3, right? 3 or not? So it is 3. So it is what answer is? This is 1 and this is 3. 3 log 3. No impact on 0? 3 log k minus 1. So if I put J equals to 1, then it is going to tell me what the log J minus 1 or suppose J equals to let us assume nothing J equals to 2 answer should be J equals to 2 answer should be 3. J equals to 3 answer should be J equals to 4 answer should be not known. So what would be the? 3 log J minus 1. Log J? 3 log J. This is the thing you want to tell? No, it is a 3 log J. 3 log J minus 1. So it is 3 log J. If J equals to 1 it is a 0 and 2 it is 3 and 3 and 4 it is 6 and so on it is there. So it is 3 log J. So the 2 column merge is done because that you want the data should be in such a way that all the elements in the first row smaller than all the elements in the second row and so on. So this idea is used in the horizontal merge. So the elements you have in this form such that it forms a biotinic sequence and we bring this data here. So initial if you remember this forms a biotinic sequence now you will observe that these elements and its these elements this has come here. This also will form a biotinic sequence agreed or not? Yes or no? No. See you have 2 biotinic sequence right. Now if this is an increasing order then this column is also an increasing order. If this is decreasing order then this column is always decreasing order. Now if I bring this data here this forms a biotinic sequence right because this was increasing order this was decreasing order agreed. Similarly if I bring this data here I get another biotinic sequence here and so on with which also and also if I merge the 2 column merge here then all the smaller elements should be here next smaller elements will be here and so on. Yes or no? Yes. Similarly is the case okay. So what I do? I rejected element I send back again. These rejected elements are rejected elements in the first row right. So they are not here these elements are smaller than these elements. So all these elements will be smaller than these elements. Similarly all these elements will be smaller than these elements. So even if I am sending the rejected elements these elements are smaller than these elements and also each of this will form a biotinic sequence this will form a biotinic sequence okay and then you do the simple row merge on each of them you will find the sorted sequence. So let us consider first one example before we discuss the algorithm 16 plus 16th okay. 1, 2, 4, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 19, 20, 21, 23, 24, 25, 28, 29, 30. So you have suppose this 32 elements and this form increasing order this form decreasing order right and you observe that this whole thing is a biotinic sequence. What we are doing? I am bringing these elements in the first column. These elements in the second column, these elements in the third column, these elements in the fourth column. So you get for I am not writing now this part I get 1, 31, 2, 29, 4, 26, 7, 25, 8, 23, 13, 22, 15, 18, 16, 17, 19, 14, 20, 12, 21, 11, 24, 10, 27, 9, 28, 6, 30, 5, 4, 5, 6, 5, 6, 35, 32, 3. Then I do that 2 column much for each column. What I will get? 1, 8, 9, 14, 19, 23, 24, 27, 31. What about here? 2, 2, 6, 12, 13, 20, 28, then 4, 5, 11, 13, 13, 11, 15, 18, then you get 3, 7, 10, 16, 17, 24, 25, 32. Then we send back the rejected elements to his counterpart. So I get 1, 2, 4, 3, 8, 6, 5, 7, 9, 12, 11, 10, 14, 13, 14, 13, 15, 16, 19, 20, 21, 19, 20, 18, 17, 23, 22, 21, 24, 27, 28, 26, 25, 31, 29, 30, 32. This is the rejected element I send back. Now first thing you observe here that all these elements are smaller than this element. All these elements smaller than this element. Second thing you observe now after sending back each of them is bitonic sequence and all these elements are smaller than this element, sorry, all these elements smaller than this element, and so on. So you can introduce the row merge here, row merge here, row merge here, row merge here and so on and finally you will get the sorted sequence. Yes, so you have horizontal merge j comma k. Now tell me what shall I write? Initial data is in what? You decide whether it is an RS register or RR register. Data is let us assume data is in RR register. In that case you assume the data is in RR register then what you have to do? What exchange? First thing is that compare how can you compare? How can you shift it? Because they are RR register that you have decided. So perform exchange operation in the first half, right. So first is perform exchange operation between RR and RS of PI or by PI. I is what? 1, 2, 1 or 0? 0 to k by 2 minus 1. So actually PI j I should write PI j but for all j, for all j. So you have performed that. Then data of RR from PI k to PI j not I j. I is what? I refers to what? I refers to what? Row. So I made mistake here. This should be j. This should be k by 2 plus j and for all i j is 0 to k by 2 minus 1. Then you perform two column R for each column. For each column send data of RR from PI j to PI k by 2 plus j. For all i j is 0 to k by 2 minus 1. Next perform exchange operation between an RS by PI j. For all i j is 0 to k by 2 minus 1. So I get back the acceptable elements in RR of each. So I have done up to this. Now I have to do the row march. Here you write assume there are two j biotonic sequences, sequences each of size k by 2. Some perform row march k by 2 on each biotonic sequence. So this is the steps involved in horizontal march. Now the question is coming to obtain the time complexity TR horizontal march j comma k. Tell me what is the routing? So you have TR 2 column march j plus TR row march k by 2 plus. This is whatever you needed that I have added. And what is the number? So this is one routing step and this is another routing step. So this is k. So can you tell me what is the solution for this? What is the solution for this? I have just now obtained which is 2 j minus 2. What about this one? Row march k by 2. Row march k answer is what? 2 k minus 2. Here it is k minus k minus 2 k is replaced by k by 2. Agreed or not? Yes minus 2 is constant actually plus k. So this gives you 2 times j plus k minus 2. Now what about exchange horizontal march j comma k? Whatever time you need exchange horizontal march j. So here one exchange operations, here another exchange operations. So 2 exchange operations you are doing. So what is TE? This result is what? T log j. What about this one? What is the result? 2 log k by 2 plus j plus 2. So it is 3 log j plus 2 log k. Now there is no compare operations here except here and so I can write TCHM jk is whatever time you need compare you need 2 c j plus TCR k by 2. Tell me, this one is known to you as log j. What about row march log k by 2? So you can write log k minus 1. Now you know what is vertical march and what is horizontal march. You are ready to write the sorting algorithm. So while writing the sorting algorithm you have to keep it in mind in the bitering sequence. So one sequence will be in keeping order, another sequence has to be in keeping order. First is that if you have the 4 cross 4, this is a bitering sequence, this is another bitering sequence. So you will be arranging them in this form. Yes or no? So that this forms a bitering sequence, then you will be merging this one. So first it is horizontal march, you have to do vertical march, horizontal march, vertical march and so on. And every time you have to keep track, whether I have to keep it positive or increase or decrease. So to do that I need to know what is the stage number. I need to know a sign function to tell if plus 1, if it is in increasing order. How I will compute the sign function? We will decide later on if it is decreasing order. So I need to know the stage, first stage, second stage and so on and a sign function. And the second thing I need to know to tell that your horizontal march k cross k, k cross 2k vertical march 2k cross 2k. What it means that first time this way consisting of two elements. In the second stage, in the first stage you will get another one that instead of vertical, horizontal march you will be making vertical march afterwards, which sign becomes 2k cross 2k. In the second phase you will get this side 2 rows but 4 columns and then 4 rows 4 columns and so on. So k will be that pointer. Initially, initially we assume that k equals to 1, s equals to 1 and you are interested to sort m cross m elements by m cross m processors in two dimensional mesh. Every processor contains one element. So you have m cross m processors, every processor contains one element, you are interested to sort this m square elements. k equals to 1, first stage s equals to 1, while k is less than m, assume there are several server arrays, each of size k cross 2k, perform horizontal march k cross 2k, then stage is increased by 1, assume several server arrays, each of size 2k cross 2k, use vertical march 2k cross 2k, you increase s by 1 again, next stage and k by 2k. So this is your main algorithm. So you observe this and this stage is some array, some bitonic sequence and this stage is some array is, is some array of size 2k cross 2k is a bitonic sequence. Here I have not told about the sign function, whether increasing order or decreasing order, then I will discuss separately, right. If it is the case, then what is the time complexity? T, main algorithm, size m cross m, number of routines. If I start, there are two ways you can compute, you can start 1 cross 2, then 2 cross 2, then 2 cross 4, then 4 cross 4, different somehow and so on, right. Instead of doing that, let us think this way, suppose I get, suppose I get this is a sorted sequence, this is get a sorted sequence, this is get a sorted sequence and so on, so that this is the bitonic sequence, this is another bitonic sequence, right, and then you available what is the fatal margin, logical mode, you will be getting the total sorted sequence, agreed or not? Suppose I assume that, this is sorted sequence, this is sorted sequence, so that this is a bitonic sequence, this is sorted sequence, this is sorted sequence, this is aborganic sequence, you call, you call So can I write here the time complexity for this is nothing but TR main algorithms m by 2 cross m by 2 plus TR horizontal march m by 2 m plus TR vertical march m, m. So I am just instead of looking for bottom of approach I am thinking about the top down approach I have some recursive method I have sorted this, sorted this, sorted this and sorted this. Now this is a binary sequence so make it as 481 which needs horizontal march algorithm so this is the time complexity simultaneously you do this then you combine these two using the vertical march which is the time needed for doing this is this. Now this gives you TR m m by 2 m by 2 plus what is the time we have already obtained j plus twice of j plus k TR m m by 2 m by 2 plus 3m minus 4. Now assume m is equals to 2 to the power k or 2 to the power m equals to, oh you add then what is vertical march vertical march question same manner this one vertical march and vertical march is row march plus column march 4 m minus 4. So it is 4 m so this is 8 this is 7 and assume m equals to 2 to the power what is the solution now TR m m by 4 cross m by 4 plus 7 by 2 m minus 8 plus 7 m minus 8 m equals to 1 answer is 0 right so up to m equals to 2 you have to go that is l equals to what is the answer this is 2 square TR m 1 comma 1 7 2 to the power what I will write is it okay sure this is 0 tell me the solution now 7 m 1 plus minus 8 l and this is 2 to the power l minus 1 3 to the power l minus 1 is 7 m m minus 1 2 to the power l minus 1 divided by half into 2 minus 8 l so this is nothing but 14 m into m minus 1 minus 8 log l now you have t exchange t exchange main m by 2 m by 2 plus t exchange horizontal march m by 2 plus t exchange vertical march m by m what is the just now we obtain 3 3 log m by 2 plus 2 log m okay what about this one 2 log m square that is 4 log this is this gives you what 3 log m 3 log m plus 2 pi plus 9 log m t em m by 2 m by 2 9 log m minus 3 so what is the solution for this 9 log m tell me minus 3 plus 9 log m by 2 minus 3 plus 9 log m 4 minus 3 9 log m here by m right okay so it is 14 m minus 1 minus 8 log m is it okay what about this one this is okay tell me this is okay this is okay right then what is the answer how many log m 9 log m l such right l such so log square m minus 3 times log m minus first one is 9 second one is what 18 so I should take 9 out 1 plus 2 plus l minus 1 so I get 9 log square m minus 3 log m minus 9 log m log m minus 1 so you get 4.5 log square m plus 7.5 minus 4.5 plus 1.5 log square now tell me what is the number of comparisons 1 is first one is log j right log m log m by 2 minus log k minus log j minus log k minus log j minus log k minus log j plus log k and what about vertical one log j log j plus log k right so this gives you 233.5 34 1 2 3 4 log m minus 2 minus 2 plus 4 times log m by 2 minus 2 4 times log m by 2 to the power l minus 1 minus 2 how many log m's 4 into log m into log m whether log m right 2 log m minus 2 times log m minus 1 plus so 4 times log square m minus 2 log m minus 4 times log m log m minus 1 divided by 2 so 2 log square m only right such a simple one good so this is about the number of comparisons you need now the fine thing only because you have to interchange still can you give me 2 3 minutes or no so I am here to cover in the next class I will cover the fine function