 So, we will start with the next couple of lectures. So what we are going to do is we are going to analyze selection sort that we had studied last time. So, here is a quick animation of selection sort just to recap what exactly happens. So, this is an array of 6 integers and we want to sort it in decreasing order. So what we are going to do is we will first find out the maximum element that is 27 and we will swap it with the element at the top of the array. Then in the remainder of the array, we will find out the maximum element which happens to be 25 and then we will sort it with we will swap it with the next position in the array, then 24 swapped with the next position in the array, then there is another 24 which gets swapped with the next position and finally, there is 18 and 17 which are already in the right positions. So, that is selection sort and finally, at the end of it you have the entire array sorted in decreasing order and just to recap this is how the selection sort function in C plus plus looks like. So, there is this loop in which current top is gradually changed from 0, 1, 2 all the way up to n minus 1 and the part of the array between current top and n minus 1 is considered the unsorted array. So, in every iteration of the loop you find the index of the maximum element in that unsorted part of the array and then swap it with current top and what does find index of max do? It really iterates from the start index up to the end index and if it finds an element which is greater than equal to the current max index, it just registers that and swap is of course, very standard. So, everybody is up to speed with this right. So, now what we want to do is we want to count the number of basic steps involved in selection sort. So, what is a basic step how do we define what a basic step is? So, you can roughly think of that the time the total number of steps and hence the total time taken for sorting an array should have some dependence on the number of elements in the array. The time it takes to sort an array of size 2 should certainly not be the same as the time it takes to sort an array of size 2 million. So, there has to be some dependence on the size of the array and that is what we are trying to analyze over here that how much time or how many steps does selection sort take to sort an array of size n, but of course you know in the selection sort function as we saw there are several things happening we are reading elements of arrays comparing them updating some other stuff and all of that. So, what we are going to do is we are going to group together operations which the time taken for which is independent of the size of the array and it is some kind of constant. So, for example, to read two elements of an array a to compare them and to update the other variable current max index this might take 20 nanosecond on some computer or 50 nanosecond on some other computer or maybe 1 millisecond on third computer. But this is independent of the size of the array no matter whether the size of the array is 1 million or 1 billion the time taken to do these operations is going to be fixed for that computer it may vary from computer to computer it may be different from what it is going to be today from what it is going to be you know 5 years down the line it may become even faster, but nevertheless this is some time that is taken for performing some computational steps and the time taken for this is some constant that depends on the technology that we are using for our computer today and it is independent of the size of the array is that clear that is what we call a basic step it is a step for which we know that there is a certain unit of time that is required maybe 20 nanosecond maybe 50 nanosecond whatever, but it is independent of the size of the array and this unit can keep changing as computers become faster as we have more advanced technology, but nevertheless this is one unit that is independent of the size of the array fine. Similarly to swap to specified elements of an array this time does not depend on how large the array is it just depends on how fast your computer is running right. So this is so these are things that we are going to consider as basic steps and you know I mean maybe incrementing i whatever checking whether i is less than n all of those things you can bunch in inside these they are also as long as I say that I am considering the time taken for one comparison of i to n or for one increment of i it is independent of the size of the array it is independent of the size of the problem is that clear so that is what we mean by a basic step it is a certain abstract notion, but a basic step means to do a computation it takes to do that computation involved in a basic step it takes some fixed amount of time and that is independent of the size of the problem the input problem ok. So we are going to count how many such basic steps that needed in selection sort and in particular we want to count what is this number of basic steps as a function of n ok. So let us see how this might work so here is our array of size 6 so n is 6 initially current top is there at 0 and then we want to find the index of the maximum element in the part of the array between a 0 and a 5 right. So that is the call find max find index of max array a starting position 0 total 6 elements and what is going to happen when we execute this function find index of max we will start off with current max index start i everything over here at 0 and then we take one step wherein we increment i and check whether 18 is greater than 24 18 is not greater than 24 so current max index stays there we take the second step is 17 greater than 24 no so current max index stays wherever it was 25 was indeed greater than 24 so current max index gets incremented and then 27 is greater than 25 so current max index increments again and finally 24 is not greater than 27 so current max index stays there and i increments there. So how many steps did we take so starting from here this was one step two steps three steps four steps five steps and in each step what did we do we read some element of an array compare it with some other element incremented something updated something basically a fixed amount of time in each of these steps and there are five such steps that we have to take right. So we used up five basic steps and in general you can see that if this array is of size n then we I mean here the array was of size 6 so we took five basic steps with the arrays of size n we will start from the 0th element and we will have n minus 1 subsequent steps right so in general n minus 1 and after that what do we do we have to swap this fourth element which we found out to be the maximum with current top right and that swap also involves reading an element of the array copying it somewhere writing some element of the array so it is some fixed number of computational steps takes a fixed amount of time so we will count that as one basic step so totally we have used n minus 1 plus 1 basic steps so far right this is one iteration of the selection sort loop when current top was 0 what is going to happen after that so basically we have just finished one iteration of this loop what is going to happen after that current top will become 1 and will iterate again so when current top becomes 1 we are here and what does the next iteration do it basically calls find index of max in the array starting from position 1 and what is going to happen there we will start current max index and i and everything over here and then we are going to increment i and 17 is not greater than 18 so current max index stays there will increment i since 25 was greater than 18 current max index gets incremented does not get incremented and does not get incremented because 25 was less than 24 in both cases so current max index stays there once again how many steps did we take starting from here so this was 1 2 3 4 so we now have 4 basic steps and if you generalize it to n elements if there were n elements in the array and we are already done with the first element so current top is at 1 now so it is easy to see that we are now left with n minus 2 steps right so n minus 2 steps and then of course we have to swap so that swap itself is going to be considered as 1 basic step and so what are we left with n minus 2 plus 1 basic steps okay now after that when current top reaches the second index right so here it was n minus 1 plus 1 here it was n minus 2 plus 1 so now it is so n minus 3 plus 1 basic steps at the next position and finally when I reach the n minus 1th entry there would have been n minus n minus 1 plus 1 basic steps so this is fairly straightforward to see so what is the total number of basic steps that we used so you add up all of those expressions n minus 1 plus 1 plus n minus 2 plus 1 and so on and if you do some simple algebra it turns out to be n minus 1 times n plus 2 by 2 the important point is that this expression has a quadratic dependence on n there is an n squared in it right so this increases quadratically with n and what do we mean by that so here I have just tried to plot how n minus 1 times n plus 2 by 2 looks like for values of n from 10 to 100 and you can clearly see the outline of a parabola over here right because this is a quadratic function right so is that good enough I mean is the number of steps that we are seeing that to sort an array of size 100 we will need some 5049 steps and then it is going to increase quadratically is that good enough it turns out that it is actually pretty bad and so this can indeed be I mean if if each step takes whatever 50 nanoseconds and if the number of steps is going to grow quadratically then the total amount of time that you need is also going to grow quadratically right and that is not good news because it basically means that we cannot use selection sort for sorting very large arrays and here is a simple illustration so suppose I have 1 million data items each with a score and by the way 1 million data items is actually a very small number in today's world if you do Google search with you know any reasonable thing let's say you know independence day of India or something you'll come up with around 10 million pages so 1 million is really a small number in today's world and if I have 1 million data items each with a score and if I want to sort these data items with respect to that score how many basic steps are required so if n is a million then n minus 1 times n plus 2 over 2 is roughly 5 times 10 raised to 11 and if each basic step takes 20 nanoseconds so this is really almost at the edge of today's technology if you want to do two array reads from memory compare them and write back one something else back to memory I think this is the fastest computers available today will probably be able to do it in around 20 nanoseconds so the computers that you are running we are running in the lab which will take much more time than this but even if I take that that if you if each basic step takes 20 nanoseconds and then if you multiply it by this number you find that you need approximately 2.78 hours to sort an array of size 1 million which is not large by today's standards so clearly this is not going to be very useful for sorting large arrays if I take hours to sort I mean it's it's like you know 2.78 hours is a movie almost so you hit the enter button on your computer go see a movie and come back and then it says it's still crunching it's not yet sorted it right so that's not good enough can we do better fortunately we can do much better and in fact what we will study in the next few lectures is that it can be done in rough roughly n times log n base 2 basic steps as opposed to n squared some function of some function like n squared force in selection sort you can actually do it in n times log n base 2 basic steps and now if you figure out how much time it would take to sort that same array of size 10 raised to 6 we'll find that in a few seconds can be done so therefore what is important is that sorting is a real world problem we'll have to solve this problem we found out one method to do it which was selection sort and it was working fine it was indeed sorting and it was doing something nicely we could program a computer to do it but it turns out that it is not good enough it's not fast enough its performance is not up to our expectations and so we'll have to now learn about other ways to solve the same problem to sort an array of numbers but I mean these other ways should do something cleverer should do something smarter such that the number of basic steps required can come down significantly so that we can then actually use it for doing real world for sorting real world data okay so is that fine is there in doubt about this so in the next part what we're going to do is we're going to study another way of sorting this the same array of integers and we'll see that this sort this way of sorting which is also called merge sort works very very differently from selection sort I mean there are some similarities but by and large it's very different and in the next class we'll actually do an analysis of merge sort and we'll see that it actually achieves that n times log n base 2 probably with some constant multipliers 2 times n times log n base 2 or something but roughly that so so this is essentially saying now you have learned programming now you have learned how to solve a particular problem but we are not happy with that solution it's not that we cannot program or computer to solve that problem we can program or computer to do selection sort but we're not happy with that solution it's not solving real world problems so let's do something smarter and then of course we want to program it on this also onto computer okay so what was the intuition behind selection sort given a particular problem let's say to sort an array of size 6 we solved part of the problem basically we solved the problem for just a one element thing we said okay let me find out what should be the top most element here okay and then we were left with the remaining five elements and we said that that's a similar but simpler problem to solve similar because it's still sorting simpler because now I have n minus one elements five elements to solve so if you see the basic principle underlying this what we are saying is we want to go from a bigger problem to a smaller problem and by solving the smaller problem and combining it with something else that I've already done I want to get a solution of the bigger problem okay so this is a general paradigm in computing where you know the I mean this is kind of this repeats in several I mean those of you who are going to study computational techniques further you see that this theme repeats several times it's it's one of the very well known techniques for solving difficult problems so what you do is you take a large problem a complex problem and you break it up into small parts okay but of course you've got to break it up into small parts in the right way such that if I give you the solutions of these small parts you can combine those solutions back to get a solution of the bigger problem so it's too difficult to solve a big problem let's break it up into small parts if that is too difficult let's break those up into smaller parts if that is also too difficult let's break them up even into smaller parts until we find that the problem is small enough simple enough that we can solve it and let's solve the simple problems and let's combine the solutions of the simple problems together to get the solution of the original problem so that's the overall idea is also called divide and conquer and I'm sure all of you know that the British basically used this to rule India for a very long time so we are going to use this to conquer difficult problems so how do we sort using divide and conquer right so always keep at the back of your mind big problem we don't know what to do let's break it up create some smaller problems out of it so we'll just break it up and create two smaller problems out of it and how did we break it up by just putting a line over here by just breaking off that array over here so 24, 18, 17 goes to the top sub array 25, 17, 27, 24 goes to the bottom sub array and we've basically broken it up into two parts now these two parts are themselves similar but simpler problems right I still have to sort those two parts so they're similar but they're simpler because they just have half the elements compared to the previous problem and how are we going to sort them so let's not worry about how we're going to sort them individually now let's say by some means maybe by selection sort or by manually inspecting it I have been able to sort the top sub array turns out that this was already sorted so not much needed to be done and let's say I have also been able to sort this bottom sub array over here by some means after I have sorted these this is a sorted array of size 3 that's a sorted array of size 3 but I really wanted to get a sorted array of size 6 so how do I take these two sorted smaller arrays and somehow get this larger sorted array out of it so clearly if I put this array after this it's not going to work because 17 will appear before 27 in this particular case if you put this array on top of that array it will work but in general it need not work because this array will have some of the elements this array will have some other elements they are individually sorted but when you put them together they may need to get interleaved right and this is one sorted array this is one sorted array but only when you interleave them maybe you'll get the overall sorted array so that's what we're going to do we're going to take these two sorted sub arrays and we're going to merge them you will see how to do this merge but we're going to merge them to create the overall sorted array and that's what is going to be our basic idea that will divide the problem then we'll somehow sort the smaller sub problems and then we'll merge the solutions of the smaller sub problems to get the solution of the larger is that clear three basic steps or three you know key steps over here so the first step is to divide an array of size n into two arrays of size n by 2 how difficult is that this is actually extremely easy just calculate the index of that middle element and 0 to that middle index is the first sub array that middle index to the index is the last sub array and of course if that array is not exactly if n is not even then you'll have one more element in one half than the other half so let's let's not worry about that and then we have to sort each of these sub arrays of size n by 2 how are we going to do that should we apply selection sort or should we do something else let's postpone that for the time being what we're going to now focus on is to see how we can merge these two sorted sub arrays each of size n by 2 into one bigger sorted array of size n so that's our objective we want to see how we can merge two sorted sub arrays so the important point is the two sub arrays that we are trying to merge are already sorted so we should make use of that we should exploit that and we should then try to merge them into one larger sorted array so here is sorted sub array 1 here is sorted sub array 2 and okay so how are we going to merge these two sorted sub arrays so remember that this is sorted already in decreasing order and the other guys also sorted in decreasing order which means that this is the maximum element of this sub array and this is the maximum element of this sub array right so this is the maximum element of one sub array and that's the maximum element of the other sub array then the maximum of those two elements should be the maximum element of the entire array right so that's what we're going to do we are going to compare these two elements and figure out which is maximum and that should be the maximum element of the entire array so since 27 is greater than 24 so 27 moves to the maximum position for the maximum element and of course now that we have taken care of this element we can reduce our whatever index or arrow to the next element so now we are looking at this sub array 25 24 which is already sorted in decreasing order and we are looking at this sub array which is also sorted in decreasing order so once again if this is the maximum element of this sorted array and this is the maximum element of this this part of the sorted array then if I just take the maximum of these two I should find the next element right so that's what we're going to do 25 is greater than 24 so 25 comes there and then that index increments there and then finally we check whether 24 both of them are 24 so it's not that one of them is greater than the other so let us take the top one with the top array has been waiting for a long time so let's take one from there and so the index there comes down right and after that it's the same thing we have to check which which is greater than what 24 is greater than 18 so we pick up 24 put it there and now we are done with the second sub array we have put all the elements of the second sub array in the rightful place and note that there is a 27 25 and 24 there is one element of the first sub array has gotten inserted in between right and now of course since we have only one sub array remaining and their elements are already elements are already sorted in freezing order we can just put those elements here and that gives us the overall merged sorted array is that clear so I hope you noticed how this arrow came only down for each of the sub arrays it did not ever move up it only came down it only kept moving down I was either moving down once the arrow for one sub array or the arrow for the other sub array at no point of time did I have to go back and retrace what I did earlier right so therefore that's how we're going to merge two sorted sub arrays of size n by 2 we're going to make one pass over each one of them and put the elements in the right order well so we had postponed this problem how do we sort each sub array of size n by 2 so now should we use selection sort or should we use some other thing so here we can step back and just think about what we're originally trying to do we were originally trying to sort an array of size n and now I'm required to sort an array of size n by 2 so it's really a very similar problem and so can't we use the same steps that we used to solve the original problem which basically means take this array of size n by 2 break it into two parts somehow sort those two parts and merge what is that somehow sort those two parts take that and again break it into two parts and keep doing this right so this basically suggests I'm going to use the same technique to solve a simpler version of the problem and what is the termination case case of that recursion so of course when there is a size 1 then everything is sorted so I don't need to break it up further so here is how it's going to work divide and conquer in action for sorting our array so we'll first break it up into two similar but simpler problems by just cutting this array at the midpoint okay now how do I sort this array this is too difficult a problem for me so I'll break it up further and because it had three elements so when I break it up one of them will have one more element than the other and now I've got to sort these subarrays how do I sort that subarray since that has just one element that's easy I know it's already sorted how do I sort the other subarray it's too difficult it has two elements I know how to sort sort arrays of only size one so I'll break it up for this two arrays of size one and then I know how to sort arrays of size one they're already sorted nothing to be done and then I'm going to merge them back right so when you merge those two back you get this so now you have this sorted subarray this was sorted because it was trivially sorted because of size one and this sorted subarray and now we've got to merge them back into a sorted subarray over here and then we've also got to solve the problem for this but once again this is too difficult let's break it up into smaller parts and then one of these smaller parts I know how to solve so that's done the other smaller part is again difficult so break it up into even further smaller parts and then I know how to solve each of those cases and then I just merge them back to get this and then I merge those back to get this and finally emerge these two back to get this and that is the sorted array and this entire process of going from the unsorted array on the left to the sorted array on the right by repeatedly breaking it up into two parts until we reach the case where we were left with an array of size just one and then we said that we know how to sort that array and then we just merged everything back so this process is called merge sort and it's kind of obvious why it is called merge sort the key step here is actually merging the other step is just breaking it up breaking it up breaking it up until we reach arrays of size one right so the key step here is really merging so this called merge sort so are there any doubts on this part of the lecture no okay good they're dividing into three parts there are no three parts right every problem is yeah so you merge them and you get this is now one sorted array one sorted sub array this is the other sorted sub array so once again you have two sorted sub arrays and you want to merge them yeah so once we merge them we get one sorted bigger array but now this bigger array happens to be a sub array of the original thing so now this this is one sorted sub array that is another sorted sub array and then we merge it back so at every step I'm saying that there's a more difficult problem let me break it up into smaller problems once you solve the smaller problems I'll just combine them to get my solution but how do I solve the smaller problem again break it up okay fine so all of you have the answer sheets okay good so let's start the quiz okay so this is the first question the number of basic steps to sort an array of n elements by selection sort grows quadratically within linearly within logarithmically within exponentially within which of the following is or are not true about merge sort can be implemented as a recursive function it's an example of computing by divide and conquer it proceeds by finding an extreme element and sorting the remainder of the array and it does not use any comparisons so which is or are not true go to the next one in merge sort the merging of two sub arrays happens before sorting either of the sub arrays after sorting both the sub arrays after sorting one but before sorting the other sub array so which of the following cannot be considered a basic step when analyzing the performance of selection sort swapping two array elements comparing one array element with another reading an element of an array finding the index of the maximum element which cannot be considered a basic step the last question will sorting an array of size n by recursive merge sort the termination case occurs when the size of that is even size of that is odd size of that is 1 size of that is exactly n by 2 okay so now you should turn in your answer sheets to the TS please collect the answer sheets and bring them here okay so now what we're going to do is we are going to try to solve some practice problems so here is our practice question we have seen part of this question in the last class we want to follow up on that remember in the last class I had actually drawn it out by hand but here I've tried to put it slightly more formally so we have an unsorted array a containing n elements okay and that could be our unsorted array for example and we want to modify selection sort so that we save some computation last time we said we want to save some computation but now that we know what a basic step is let's say that we want to modify selection sort so that we save some basic steps compared to selection sort as studied and at the end of it a should be divided into three sorted segments that the sorted segments could be of any size remember we discussed about this in the last class just before the end of the last class right so so for example given this array input array it is fine if the output of my sorting procedure is this because this array can be divided into three parts so is that each of the parts are sorted in increasing order 039 is sorted in increasing order 1678 is increasing order 22345 is an increasing order however the whole array is not sorted in increasing order 9 appears before 1 and 8 appears before 2 and so on is it clear so if you run selection sort on the entire array then it will just sort everything so we are saying we don't need that it's okay if that modified selection sort keeps things sorted within three segments of the array it basically divides their into three parts and within each part it ensures that everything is sorted this is the question we looked at last time so what we're going to do today is simplify it a little bit and say that okay let's not worry about three it's worry about two parts so we want the modified selection sort to order the elements in the array such that the array can be broken into two parts and in each part it is sorted is the question clear to everybody so now we have already seen that the number of basic steps needed to sort an array of size n by selection sort is something like n-1 times n plus 1 over 2 the question is that how many basic steps are going to be needed if you divide the array into two segments of size m and n minus m so remember the our modified selection sort is required to have two sorted segments of the array the whole array need not be sorted right and we are expecting to get some savings in terms of the total number of basic steps so we are saying that suppose these two segments are of size m and n minus m so how many basic steps would be needed and more interestingly for what value of m is the number of basic steps minimized what should you choose the sizes of the two sub arrays such that the total number of basic steps needed to ensure that these two segments are sorted by selection sort is minimized and I mean you're free to do it using whatever way you want but I mean this is basically a minimization problem right and once you find an expression for the total number of basic steps in terms of m and n you can apply your high school calculus techniques find out for what value of m is it minimized but I want you to do it okay and actually figure out how many number how what should be the size of m and then for those of you who can complete that I would like you to apply the same technique to figure out how to solve the problem with three segments is the problem clear to everybody we are saying that we don't need the whole of selection sort we just need selection sort to break that into two parts is that each of them are sorted so what should be the sizes of these two parts such that the number of basic steps is minimized and similarly if I want to break it into three parts as that each of the three parts are sorted what should be the sizes of three parts first write down your solutions then we'll ask you to discuss with your neighbors and then we'll try to project some of the selected solutions away so I guess you can this you know exchange your copies with your neighbors and discuss how many of you have solved the case for two segments and how many of you got n minus one and one as the answer nobody n by four and train by four as the answer nobody okay and by two okay so okay just swap it with your neighbors and see if and how many of you got the answer for the three segment problem you actually solved it and found the answer not saying I think that this will be something nobody has got you have got solution for three segment you've actually solved it and found the answer okay okay so I've just taken Gautam's copy I'm sure a lot of you would have already done it like this so you just the two the two parts are of size m and n minus m so you just form the expression for the number of basic steps do some simplifications and then you want to find the minimum value of that so you differentiate it with respect to with respect to what m right so and when you differentiate it with respect to m and set it to 0 is it guaranteed to give you the minimum value can also give you the maximum value right so has has anybody I didn't see anybody computed the second derivative and shown that it's really the minimum you have okay I should have okay great so you should do that because otherwise you know the minimum value may be at one of the extremes okay so so this is differentiated set equal to 0 and then it turns out to be n by 2 roughly so for the three segment part you basically do the same thing the three parts are of size ml and n minus m minus l and you form this expression simplify it and then you do what differentiate it with respect to if you take the partial derivatives now this is a function of two variables right so if you take the partial derivatives and then equate the partial derivatives to 0 and did anybody compute the second partial derivative okay you did okay and you can show that that is indeed the minimum okay so that happens at m by 3 so can you give this okay so I hope everybody got m by 2 right once you form the expression you differentiate that's fairly easy the m by 3 part is also n by 3 part is also easy just that the expression becomes a little bit more complicated so now here is the second question you have an array called birthdays and let's say there are thousand students and we want to store the birthdays of thousand students in this array and each birthday is going to be stored like this it's an integer in the year month day format okay now we want to sort the array in order of earliest to latest birthday which means that a birthday on 1st January regardless of the year is earlier than a birthday on 1st February which is earlier than a birthday on 3rd February and so on right basically you can think of this as we want to sort this array in this order so that we know which days we can celebrate birthdays in the class okay so we don't care which year the student was born as long as there is a birthday we can catch the student and say please give us a treat so so for example 1984 6th February should be ordered before 1983 21st March although 1983 happens before 1984 because 6th February happens before 21st March is that clear this 6th February because the day 6 the month is 2 is 21st March because days date is 21 and month is 3 and similarly 1985 22nd March will be after that okay now remember in last class we actually talked about this operator the ordering operator and this is an example of that ordering operator which is neither the less than or equal to up on integers not the greater than equal to on integers right this integer is certainly not less than or equal to this okay this integer is in fact greater than equal to this but then this integer is not greater than equal to this, this in fact less than or equal to this. So, here the ordering operator is neither less than or equal to nor greater than equal to, it is an ordering operation that we have defined which is the ordering of birthdays in a year. Is it clear how we want to order this array? So, in the morning's class actually one student raised his hand and said what happens if you have a birthday on 29th February. So, I said I don't know. So, does anybody here have a birthday on 29th February? No? Does anybody have a friend or a relative whose birthday is on 29th February? Oh, there are lots. So, what do they do? Do they celebrate birthdays once every four years or they celebrate on 28th? So, well, so if you have a birthday which is 29th February, maybe you can I mean from people with experience they are saying there's a celebrate around 28. So, we can consider it the same as 28th February and that's how we want to order them. Or maybe for the time being let's just assume that no student in this class which is true right no student in this class has birthday on 29th February. So, let's assume that we won't deal with that case. So, here is how selection sort for this problem might look like there is a typo here this should have been find index of min, but this function is looking ok. So, find index of min this is the loop that we had remember for selection sort to find the index of I mean in the in the slides we had finding the index of the maximum element in the array. Here we want to really not find the index of the minimum element in the array. Here we want to find the index of the earliest birthday in the array ok. So, we are asking what should you put in over here what should be the expression involving birthday is I and birthday is current min index that should go in there. So, that finally this is the only place where an ordering operator is involved in selection sort remember the other operation is just swapping with current top that does not involve any ordering operation. So, if I can write this part correctly then I can just use the existing code for selection sort and it will already sort my array in the desired order. This is the only place where we use the ordering right and now we are asking what should be the ordering there what should be the expression there involving birthday is I and birthday is current min index ok. So, please this is also not a very difficult question fairly straight forward. So, I want all of you to write it down and then we will have some discussion we will look at the expressions that you write and we will have some discussion on that ok. So, how many of you have solved it hopefully all hands should rise how many of you have solved it ok good. So, I think most of you have. So, this was a fairly straightforward question, but there is something I want to highlight with this. So, this is Ranvijay's solution. So, he says that if birthday is I percent 10,000 is less than birthday's current min index percent 10,000 right. So, why is it percent 10,000 because that will pick up the last 4 digits right which is the month and the day and we are basically sorting with respect to those last 4 digits, but now here is a question. So, how many of you wrote less than and how many of you wrote less than or equal to. So, I mean of course both are correct for purposes of solving the problem, but suppose I write a program which has less than and I write a another program which has less than or equal to I feed the same array to both of them will the result the output of both the programs be the same ok. So, what will be the difference? Those birthdays having the same month and date if you consider all those birthdays which are having the same month and date then the birthday which is the most down the array will be listed first in the sorted array which is considered last in the comparison will be sorted will be listed first in the sorted array whereas, if it is just less than then it will be the opposite case ok. So, does everybody agree with him? So, what he is saying is that if you put less than then. So, I said. So, suppose in the array I have all the birthdays the same just the years are different right. So, if we put just less than then the array will be the same and if you put less than or equal to then it will be reversed. Then it will be reversed ok. So, does everybody agree with him on that? So, what? So, suppose I give you an input array in which I mean this is a very strange kind of class in which everybody has all the 1000 students have the birthday on the same day same day same month ok just that the years are different and possibly different. So, the question is if I put less than birthdays I percent 10000 less than birthdays I percent birthdays whatever current bin index percent 10000 how will that input array get sorted? So, your claim is that it will just get it will stay the same and if I put less than or equal to it will get reversed ok. So, if it is less than it will get it will remain the same how many of you agree with that? Is the question clear? So, in the entire array birthdays all the months and days are the same. So, the only difference is years ok and now we are sorting it using the program selection sort and over here we just saw that one of the solution suggested was birthdays I. So, let us say my array is array birthdays is something like this birthday 0 is whatever you know let us say 1985 1st January and birthdays 1 is 1986 1st January and birthdays 2 is 1984 1st January. So, my array is just like this everything is 1st January, but here the years are different right and the two options that we saw was if birthdays I is less than so, birthdays I percent 10000 this is some current bin index whatever percent 10000. This was one solution and the other one was if birthdays I percent 10000 is less than equal to birthdays. So, in an array like this where all the months and days are the same if I run selection sort with this what is going to happen? Yes same as the input the output will be the same as the input ok. If I use the less than sign on this array and if I use less than or equal to reverse. So, why do you think it will be reversed? So, let us first take a poll from the class. So, what is your name Shrikanth. So, Shrikanth thinks that using less than or equal to this whole array will get reversed how many of you agree with that how many of you disagree with that you ok from here some of you disagree and from here ok. So, let us ask him. Sir, the arrangement will be flipped like, but the whole series will be just reversed I am not very much convinced with that. The whole series will the array will be reversed. I am not very much convinced with that there will be some sort of flipping and all, but like one goes to the n minus 1 when the whole series getting just. So, but will it be flipped I mean Shrikanth claims that it is going to get flipped. No sir I am not. It is just the ulta of what we have now. That is not convincing sir. So, why do you think that is not going to be true? Sir, because at one go one goes to two then two goes to three and three goes to four, but two does not goes to are like. Ok. So, anybody else who said that they do not believe that the array will get reversed? Only the last element comes to the first place and the remaining things gets displaced. Displaced? By one element down. The last one goes up into the array and the remaining ones shift one place. The remaining one shifts one place? No. Oh shifts one place you are saying. Ok. So, why is that? Why does that happen? So, yeah. So, I mean you have worked it out and seen that that is happening. Yes. Ok. So, let us see. So, this element. So, if you use less than or equal to then since all of them are equal the current min index will come all the way till the end right and so it will get swapped with this ok. So, let us say the current min element here was whatever maybe this is let us say 2000 and this is 1999 something right. So, this 2000 will come over here and 1985 will come down fine. So, what we will have is this will become 2000 and this will become 1985. Now, what is going to happen in the next step? In the next step you are going to start from here right and you are going to find the current min index and what will you find the current min index to be it will be this and so now this will go up over here and this will be 1985 and this will be 1986. So, you already see that it is not flipped right because otherwise 99 should have come here right. So, now next what happens you start from here and once again 1986 will be picked and it will come here and this will be 1984 right and then 1984 will get picked and it will get put there and whatever is there will come down right. So, that is exactly what he is saying what is your name? Divakar ok. So, that is what Divakar is saying that everything will get shifted down by one position and only the last element will come to the first and what will be the final last element over here? It will be 1999 right. So, basically the whole thing as if shift cyclically everything comes down and whatever overflows goes back at the top right. So, although it appears that you know the rest than or equal to would actually flip the entire array it is not and this is something that you have to be very careful about because I mean true we may not be interested in I mean for purpose of this question we might say that all of them are the same, but then you might want to sort the same array by different criteria. So, you might say that I first want to sort them by year and then I want to sort them by this and you do not want the previous sorting to be disturbed if two things were equal by the other measure ok. So, but here the less than will actually do that, but now here is a further question I have that if you keep less than over here is it the case that the order will act I mean for this specific array where everything was the same then it preserved the order, but is it going to preserve the order in all cases? If I use less than then two elements which are which have the same month and day, but different years in the final sorted array will appear in the same order as they appeared in the original array if I use less than in this specific case it does, but in general will it. So, what do you think the answer to that is yes or no? In this specific case it does, but in a more general case will it ok. So, maybe I leave you with that food for thought this is actually a very important criterion for sorting algorithms is called the stability of the algorithm. So, if something preserves the order among equals it is called a stable sorting algorithm. If it does not preserve the order it is called an unstable algorithm and we are asking is selection sort stable or unstable ok ok.