 Okay, so apologies for the delay. We got a bit delayed. So what we're going to do is we received some feedback from the pre-midsome part of the course and based on that feedback we are doing a bit of course correction. So what we're going to do from now onwards is that you will be required to of course, you will be required to see the video lectures before coming. So roughly two video lectures instead of three we will ask you to see before coming. But on some weeks it may be three and in class we will actually go through all of the slides that are covered in the video lecture once again, but at a slightly quicker pace than in the video lectures. And the whole purpose is that if at some point in the video lectures you had difficulty understanding something or you did not understand something, you should raise your hand in class when we reach that point in the slides and we should have a discussion and get your doubts clarified. Is that clear? So we are basically going to go through the same slides but we will assume that you have already seen the slides and at the places where you found things difficult please stop us, ask the questions, get things clarified and then we will proceed. So we will do this for roughly 30-35 minutes. After that we will have the quiz, the quiz that we normally have. So what we will do today is so you can think of it as a fresh lecture. So please pay attention and but please go back and see the video lectures. So this is basically a simple table containing roll numbers and marks in different quizzes of several students and given such a table we might have several questions we want to ask, we might want to rank the students in decreasing order of total marks or we might want to find who are the top few students in quiz one or who are the bottom few students in quiz three and so on. So the different kinds of questions that one can ask once you have presented with data like this and what we will study today is that based on whatever you have learnt so far even if you do not go into the histogram equalization and all of that there are very straightforward ways to do this and what we will learn today is one of the techniques perhaps the simplest technique to do this. So if I just focus on that problem ranking all the students in decreasing order of total marks I am basically saying that focus on the last column and figure out since it has got to be decreasing orders figure out who got the highest marks put a tag one over there, the next highest marks put a tag two over there and so on and once we have put all of these tags since we want to sort of rank them in decreasing order it might be more convenient to put this guy who got the highest marks at the top of the table the guy who got the second highest marks in the second row of the table but once you have identified which is the row corresponding to the highest marks in the total column and the next highest marks and so on you can just rearrange the rows of the tables so that the one with the highest marks goes at the top and then the next one and so on and once you present the table in this form it is actually the table is sorted the students are already ranked in decreasing order of total marks as you come down the table you have decreasing order of total marks of course what you see is that there are two students who got 24 total so which one of them appears before the other so that question remains now one can say that I do not care any one of them can appear before the other but one could also say that well if two students have got equal marks then put the one with the lesser row number first and the one with the higher row number next so we could come up with additional criteria two of them have so this is fairly natural right I mean we need to determine an order by which we want to put one before the other and it could very well be the case that now if you look at the same table I might be interested in another question saying find the top n students from quiz one scores and then what I am asking you to do is I am asking you to take the same table and now reorder the rows in a different way but earlier we had reordered the rows in terms of the total marks now we want to reorder the rows in terms of quiz one marks and what you see here for example is that while this student had more marks in total but here she had less marks in quiz one than this student and so in the ordering of total marks this student would appear before that student but in the ordering of marks in quiz one it would be reversed because we are now interested in ranking them by the marks in quiz one is that clear and once again over here you know one can have the situation that there are several students who got the same marks in quiz one which one of them appears above and which one of them appears next so we could come up with some other criteria maybe if all of them have the same marks in quiz one then rank them in decreasing order of total marks this is some other criteria one could also say rank them in order of their roll numbers or rank them in order of their quiz two marks or something but when you are trying to rank something based on a particular column over here it is perfectly possible that you might get multiple entries which have the same values in the column and then you have to decide on some additional criterion to order them is that clear? still natural problem and here is yet another example so on the same data we now want to find the least scoring students in quiz three so the smallest marks has gone to the top and then the next smallest and then the next smallest and so on and this is how things have been ordered now so it is the same data but depending on what criteria I want to use to order them depending on what criteria I want to use to order them the rows can get arranged in different ways and once again here I could have students who got the same marks in quiz three and then I could again say rank by total marks so the point I am trying to say is that when you are given some data and you are required to order the data items one after the other you have to come up with some criterion and this criterion should be such that eventually for any two data items for any two rows in the table you should be able to say which comes before which comes later so just saying total marks may not suffice because two students may have the same total marks so then you have to say how do you resolve this tie between them you can say look at quiz one marks if they are also the same look at quiz two marks something must be different between these two data items otherwise they are the same data item why are they occupying different rows of the table so in this particular case eventually the roll numbers must be different because they are different students so if everything else matches then put the roll number which comes earlier in the ordering of numbers first so basically if I abstract this out what we see is that we might have the same data but we may need to order this data by different criteria so in general I am given a collection of data items call them d1, d2 till dn and in our case each row of the table of marks is a data item we want to order the rows and we are going to think of an ordering operator so do not read this as a less than or equal to sign this is just think of it as some operator what does this operator do given any two data items di and dj this operator tells you which comes before which comes after so this is an ordering operator such that for every pair of data items it tells you whether di comes before dj or dj comes before di which one of these holds and then a specific example for example when we said that when we are ordering the rows in that table of marks we could say that I want to order things in ascending order of quiz tree marks so this ordering operator could mean less than or equal to on quiz tree marks so di will appear before dj if the quiz tree marks of di is less than or equal to the quiz tree marks of dj and if the quiz tree marks of di and dj are the same then you could say that if di has less than di's total marks and less than or equal to dj's total marks then they are good to be ordered so the point is that this operator over here should clearly specify for any two data items which one comes before and which one comes after so I could say that use the less than or equal to on quiz tree marks to sort them out and if there is a tie then use something else to sort them out but eventually I should be able to say which comes before and which comes after ok so we have seen another example where when I was sorting everything by ranking everything by in decreasing order of marks so then this ordering operator was saying is di going to come before dj if di's total marks is greater than equal to dj's total marks and if they had the same total marks if di's roll number was less than or equal to dj's roll number so you see what the meaning of that operator is I mean this operator is not just the usual less than or equal to operator this operator is saying given two data items which could be some composite data items like a row of the marks table which has roll numbers marks in different quiz, total marks and all of that given two such rows which are the data items how do I figure out which comes before what and one has to specify something like this that look at some column of it if this is greater than equal to that then you place it before that if they are the same then look at some other column if this is greater than equal to a less than or equal to place in before that is that clear so once you are willing to take this as an arbitrary ordering operator not necessarily a less than or equal to ordering operator you will see that I can now express descending order and ascending order both in terms of this descending order just means something like this A will be ordered before B if A's marks is greater than equal to B's marks and descending order ascending order will be A's ordered before B if mark is less than or equal to B's marks is that clear so this does not really make any distinction about whether it is ascending or descending this is just an ordering operator who comes before who and our goal is to order the data items such that they monotonically increase with respect to this ordering operator so we want according to this ordering operator whichever data item should come before everybody else that to appear first then the data item which should come second in that ordering to appear next and so on is that clear so I can think of ordering students by decreasing marks or by increasing marks or whatever other criteria increasing roll number decreasing roll number just by specifying the ordering operator the ordering operator just says who comes before whom it does not say somebody is less than or equal to somebody is greater than equal to say somebody comes before somebody else and how do I decide who comes before that if you define what the ordering operator is that clear it's somebody before somebody not somebody less than or equal to somebody not somebody greater than you. So, this problem is also called sorting when you are given a set of data items given an ordering operation and you want to order them such that they monotonically increase with respect to that ordering operation this is also called sorting. And I mean this is I mean you would have seen this a lot of times when you know when we do normal things for example, if I am doing online shopping and I want to purchase a ticket from let us say Mumbai to Delhi for a particular date then any of these ticketing portals will actually ask will actually allow you to list the options according to different criteria. So, they may say do you want it sorted by price do you want it sorted by time of departure number of stops and what have you right. So, this is really sorting in action there is a set of data items there are so many tickets that are available from a particular place to a particular place on a particular day. Now, I want to present that data in a certain ordered way and the user can control which way it should be ordered I mean more often than not we are worried about the price. So, we say give it an increasing order of price, but you might be worried about the duration of travel or whatever. So, the same set of data items can be ordered by different ways and it is actually real life applications use that. So, you know I mean this is as I just said. So, it allows the user to look at the top few or the bottom few because these might be the most interesting ones and what is more useful for one may be less useful for somebody else. So, therefore, it is important to sort the same set of data items according to different ordering criteria. So, we should not fix it in our head that there is only one way to sort depending on how you define the ordering operator you could come up with different sorting orders fine. So, here is another example suppose you have a telephone directory in which you have these pairs of names which are strings and telephone numbers and after that suppose I ask you to find the telephone number of a particular person from this directory. This is similar to asking you to find the meaning of a word from a dictionary. Now, if the words in the dictionary or if the names in the telephone directory were just written in some random order it would be almost impossible to find it out. So, therefore, what we often do is we expect the telephone directory or the dictionary entries to be ordered in a particular way and this is usually what is called the dictionary ordering or you know we will see later this is also called a lexicographic ordering, but I think the best way. So, everybody here has seen a dictionary. So, you know what I mean by a dictionary ordering. So, if the names are ordered in a dictionary order may be first by first names and then by last names or first by last names then by first names then given a particular name you can quickly zoom down onto you know the small set of pages where the desired name should be there and then you can just search in that set of pages. So, what you see is that I mean not only is sorting useful for applications like what I showed when you are trying to look at the same data in different ways, but sorting is also useful when you are trying to search something in a large body of data. If things were not in any ordered way if things were not in any structured way it would really be very difficult to find out a needle in a haystack. If things are sorted, if things are ordered it is often easier and once again we do this every time we look up a dictionary or a cell phone print. I mean every time you do a Google search for example, for example you could try this out I mean if you go to Google and type whatever you know independence day of India or something there will probably be more than a million entries that come up, but it is not going to show you all the million entries together. It is going to sort them rank them in a certain order. This is usually called you know they have some page ranks or whatever there is some order by which it will rank and then it will show you the top 50 pages maybe because if you were to see all those one million pages it would not be very meaningful. So, it sort of sorts it by whatever the relevance to the query that you have made and then it presents the top few. So, every time you do a Google search there is some sorting happening at the back end and that is how you see the most relevant results. So, what we will do now? So, are there any doubts on this part of the lecture? So, I think I took around 14 minutes to cover this part of the lecture. So, I think if you saw this in your hostels in the video it should not take more than 15 minutes. This fairly simple going and there is nothing very difficult about it, but in any case we are going to go through the slides and if you have difficulties please you know raise your hand ask us you know when we discuss this in class. So, what we are going to do now is we are going to go to the next follow up lecture on this. You know this is the same table that we saw and let us just focus on that one problem of ranking all the students in decreasing order of total marks. So, really I want to look at this column the column of total marks that is what that matters and I have to sort this in decreasing order. That is the core problem and for the time being let us say that if two marks are equal any ordering between them is okay, but of course you know as we just saw that we have we should indeed specify an ordering such that even if two marks are equal they get sorted by some other means. But let us just to keep things simple let us say if two marks are equal we will be fine with whichever one we place before or after. So, this is one particular way of labeling them the highest marks next highest marks and there are two third highest marks and so on and then you would like to sort these total marks or arrange these total marks in this way the highest one goes at the top next highest one next and so on. So, this is what we have already seen. Now this process of going from this unsorted array of numbers you can think of this column as a one-dimensional array of numbers of course this is a column in a two-dimensional array but just to keep things simple think of this column as just one array and we want to go from this unsorted array of numbers to this sorted array of numbers okay and the question is how do we do this in a systematic way such that we can then program a computer instruct a computer to do this and of course once we instruct a computer to do this it should be able to do this not only for this example but for any other example. I mean this example is we can at least look at it and then do all of this manually but once we have programmed it into a computer it should be able to do this even for much larger arrays which we cannot do manually. So, that is the overall objective is that we want to find out a systematic way of going from the unsorted array to the sorted array such that we can then write a C++ program to do that fine okay. So, what we are going to do now is actually something that is going to repeat several times you know when you are trying to solve a more complex problem by means of a computer program and in fact a lot of computational techniques that we study in computer science are kind of you know similar to this. So, what do we do we take a problem for example here the problem is to sort this array of six numbers and we do not solve the entire problem we solve just a small part of the problem but we say that by solving that small part of the problem we have simplified the problem to some extent and then we try to solve the simpler problem and how do we solve the simpler problem by again solving a small part of it and coming up with an even simpler problem and this way we continue. So, we will just I will just illustrate what I mean by that. So, in this case I have to sort this array of six numbers in decreasing order okay. So, what do I know that after sorting the first element here should be the maximum number right. So, let's just find out the maximum number in this entire array I am not sorting it I am just finding out the maximum number in this entire array and I am going to put it in that top position. Now how do we find a maximum number in a in a one dimensional array I hope all of you can do this now with a simple for loop you can just traverse that a keep track of the maximum and figure out at what index the maximum element appears right. Now once you know that the maximum element appears at this position you want to move it to the top of the array but there was already something there at the top of the array so where is that going to go. So, one easy way to do it is to just swap this with this this has to go up so that has to go somewhere else. So, let it come back to where whatever position was like it is. So, all of you have already also seen how to swap two numbers. So, how to swap two elements of an array should also be easy this create a temporary variable copy one of the elements there copy the other element and then copy back from the same. So, that is exactly how we are going to proceed. So, we look at this array of six numbers find out the maximum which we already know how to do based on whatever we have studied put that maximum at the top position and whatever was there at the top position will bring it down and now what I have is that I know that the maximum element has gone to its rightful position and now I have the remainder of the array where of course this 27 has been replaced by 24. I have the remainder of the array which is unsorted yet that is not sorted. However, if you notice that now my job has been simplified slightly because now I am required to sort this array of five numbers. So, I still need to sort. So, it is a similar problem like what I was trying to do before but it is slightly simpler because earlier I was required to sort an array of six numbers now I need to sort an array of five numbers is that clear? So, this is a similar but simpler problem and how are we going to solve the similar but simpler problem by exactly the same method. We are going to find the maximum element in this simpler problem put it at the top of that simpler problem and come up with an even simpler problem and we are just going to repeat this ok. So, we will say ok how do I sort this find out once again solve part of the problem which is find out the maximum element in the simpler problem put it at the top most position of the simpler problem right that position has already been occupied by 27 we do not want to disturb that the problem we are now trying to solve is to sort this array of five numbers. So, we figure out the maximum element put it at the top most position of this array of five numbers and we come up with the remainder of the array where once again as you will notice that since this 25 went to the position of 18. So, 18 has come down to the position of 25 and now I have the remaining task as the task of sorting an array of four numbers. So, I started off sorting an array of six numbers then I simplified it to sorting an array of five numbers now I am at sorting an array of four numbers and I will now continue doing this ok. So, what is the basic thing that I am using I am just using the ability to maximum element in a given array and to swap to elements of an array. So, as long as you are comfortable doing these two we should be able to repeat this thing right. So, once again in this array of four numbers let us find the maximum element let us put it at its rightful position which is the top of this array of four numbers and we are now left with a remaining array of three numbers which have to be sorted. How do we sort that once again let us find the maximum number put it up there and you are left with two numbers to sort. How do we sort two numbers same technique find the maximum number put it there and you are left with one number to sort and an array containing just one number is of course sorted there is nothing to sort there ok. So, now we are done and effectively what we have done is we have gone from this unsorted array to this sorted array in a systematic way which we want to now of course program into a computer, but the point is that we have done this in a very systematic way at every step we knew what to do right we have some we had some unsorted part of the array found out the maximum element there put it at the top of that unsorted part of the array and we got a simpler problem and we went ahead solving the simpler problem. So, this technique of sorting it is really one of the most primitive techniques of sorting, but I hope it you know it shows that whatever you have learned so far can be used to do something useful. This is also called selection sort where does the selection come from at every step we are selecting the highest element in this particular problem we selected the highest element because we wanted to sort them in decreasing order we wanted to sort them in increasing order at every step we would sort we would select the lowest element right and we go about doing this. So, as a C++ program programming problem this is how our problem might look like that we are given an array of n integers and we want to sort them in decreasing order let us say we want to do it like this and if two elements are equal then let us say we say either of them will be sorted ordered before the other, but of course you understand that when I am finally going to write the program it is not going to make a random choice of which of two equal elements to put before the other the logic of the program is going to decide which of the two elements to put before the other. If you run the same program on the same input multiple times you will get the same output it will order the same element before the same other element if they are equal right. So, here is how our program might look like I mean this is our usual stuff right we are taking the number of integers to sort we are doing some input validation. So, let us say that we are going to sort up to 100 numbers here you could change that to whatever large number you want and then we have this array of 100 integers to array a of size 100 which can store up to 100 integers to sort and what we are doing is we are just reading in the elements of this array. So, this is fairly straightforward we have a for loop which counts from 0 to n minus 1 and reads in all the elements to sort then we have to sort right and we have and since we have decided to sort by selection sort how are we going to do. So, we will say that at any point of time the unsorted part of the array is initially from a 0 to a n minus 1 next time it is from a 1 to a n minus 1 next time it is from a 2 to a n minus 1. So, at any point of time the unsorted part of the array is from a current top to a n minus 1 where current top is going to take the value 0 first then 1 then 2 and so on right. So, that is what this little for loop is doing it is saying let current top go from 0 all the way up to n minus 1 and this is the part of the unsorted this is the unsorted part of the array a current top to a n minus 1 and we have to sort just that part, but what do we mean by sorting that part? We have to find the maximum element in the unsorted part we have to find its index and then we have to sort the topmost element in the current unsorted part of the array with the element at that index that is exactly what we did. So, initially from a 0 to a n minus 1 we find the index of the maximum element swap it with a 0 then a 1 to a n minus 1 find the index of the maximum element swap it with a 1 and we keep doing this that is exactly what we did. So, this is what our program should do. So, how are we going to write this? So, we can say let us use two functions one is finding the index of the maximum element in the array a between the indices current top and n minus 1 and after we have found that we can just swap these two elements in the array. So, this is all that selection sort is about. Is everybody clear on this? Whatever we have said whatever we have demonstrated about selection sort is really this. The part the unsorted part of the array is gradually reducing 0 to n minus 1 1 to n minus 1 2 to n minus 1. So, current top is reducing from 0 1 2 and so on is increasing from 0 1 2 and so on and at any point of time given that a current top to a n minus 1 is the unsorted array I should find the index of the maximum element there and I should just swap it with a current. So, let us see how this might work find index of max this should be fairly straightforward I mean I am giving you a one-dimensional array and I am saying from current top to n minus 1 which is the maximum element. This is very similar to finding the maximum element in the whole array except that now I am saying you start from current top not from 0. So, that is what we are going to do. So, yeah. So, one point to note is that if you note here that we are passing an array as a parameter to find index of max we will also pass this array as a parameter to swap. So, when you pass an array as a parameter to a function this is how you denoted this is saying this is an integer array and all arrays that are passed as parameters to functions are passed by reference. So, in this case start and end are being passed by value, but because this is an array it is being passed by reference. So, whatever changes I do to a within this function will be reflected in the main function. Arrays when you pass as parameter to a function they are always passed by reference in C++. So, this is exactly what we do. We have the start position remember find in max is going to be called from current top up to the last element. So, we are calling that from start to end but start is the current top and end is the last element and what we are going to do is we are simply going to iterate from start up to less than end. And if the current element is greater than equal to a current max index where current max index is initialized to the first element the start element and if you find the current element to be larger than the currently seen largest element then we just note that the index of the currently seen largest element is to be i. Is this clear what we are doing? We just finding the largest element from a current top to a n minus 1 and noting its index. We are initializing that index to start which is the current top freely and then as we are iterating through the array if we find an element greater than equal to whatever the maximum we have seen so far we update that. That's good. How is swap going to work? So, once again you know since I am passing this array as a parameter to swap this is going to be passed by reference and I simply want to swap the two elements at index 1 and index 2. So, this is straight forward all of you have seen this. Just use a temporary variable temp and do this. So, that is what selection sort is above. It's this little loop over here and these two functions are doing something very simple. Is that clear to everybody? How we implemented selection sort in C++? You really needed to know nothing beyond what you have studied so far. Finding the maximum element in an array and swapping to elements is all that you need to know inside a suitably you know wrapped inside a suitably volume. So, just want to draw your attention to a couple of things here. So, this is the main loop inside find index of max. We are going from the start index up to less than the end index and whenever we find something greater than equal to we are updating it. So, one might ask that why did I use greater than equal to here? And what is the effect of using greater than equal to what would happen if I used greater than over there or what would happen if I use less than over there? So, let us see. So, suppose you know let us see what would happen with this program if I used greater than equal to here on a particular example. So, here let us say we have this array of six elements. So, n is 6 and let us say at a particular point of time current top is here. So, this is really the part of the array that I am interested in sorting. Now, this is the call that is now made. So, we are passing the array as a parameter. Who is the current top? That is from where I am asking it to start looking for the maximum index and 6 is the total number of elements. So, how are things going to start? So, I will initialize current max index to this, the currently seen part of the array and then of course start and I are there. Now, as I increases because 18 is greater than equal to 17, right? Remember, we said if ai is greater than equal to a current max, then I am going to update current max index. So, since 18 is greater than equal to 17, so current max index will now point to 18. Then once again 24 is greater than equal to 18. So, current max index will point to 24 and I mean a current max index will be 24 and once again 24 is greater than equal to 24. In fact, 24 is equal to 24, so it is greater than equal to 24. So, current max index will come here and therefore this is what find index of max is going to return, it will return 5 as the index and now you have to swap this with current top and that is what is going to happen. So, this 24 is going to go up, not this 24, although both of them were 24s, right? And 17 will come down here. On the other hand, if I just used greater than here instead of greater than equal to, what is going to happen? In the same example, we start off from here, current max index starts here and now 18 is greater than 17, so current max index will increment, 24 is greater than 18, so current max index will increment again, but this 24 is not greater than 24, it is just equal to 24. So, current max index will not increment for this, it will just stay wherever it was. So, it is now going to return 4 as the current max index. So, that is the element that is now going to swap with a current top. So, you see that depending on what operator you use there, it actually decides which of two equal elements are actually going to go first and which is going to go later, right? And of course, if I used less than or equal to, then what would happen? Then I am choosing the minimum element, putting it at the top, so then I am really decreasing them and I am really sorting them in increasing order. The lowest element goes first, then the next and so on, but then if you write a program with this as less than or equal to, then you are really finding the index of the minimum element, right? So, you should probably call these as current min index rather than current max index and I just wanted to show that here to highlight the point that you should name your variables meaningfully. If you are using less than or equal to there, although your program is going to work, it is going to sort things in increasing order, but really you should name the variables as that it is easier to understand what is it that you are trying to find, the min index rather than the max index, right? So, that is the end of this lecture. I mean, the choice of the comparison operator determines whether it is going to get sorted in increasing or decreasing order and it also determines how equal elements are going to get ordered. So, if you do not care about how equal elements are going to get ordered, that is fine, but if you do care, you have to be careful about what comparison operator needs. So, are there any questions on this part? No questions? Okay, good. So, what we will have now is we will have the video quiz. So, I hope all of you have the answer sheets. So, Firuza will conduct a video quiz. There are very three very simple questions. If you have either seen the video lectures or just stayed awake while I was saying all of this, you should be able to answer those and then we will have some practice problems, which once again Firuza will conduct. Okay. Okay, so we will start the quiz. Question number one, selection sort can be used to sort numbers in A, ascending order, B, descending order, C, any user defined order, D, none of the above. This is a quiz. So, you did not discuss the answers over here. TAs, who is the TA for you? Please know this is a quiz, no discussion of answers. Another 30 seconds move. Question number one, selection sort can be used to sort numbers in A, ascending order, B, descending order, C, any user defined order, D, none of the above. Next question? Question two, in selection sort, we reduce a problem to a smaller instance of the problem by finding A, an element at one end of the sorted order, B, an element in the middle of the sorted order, C, an element at any random position in the sorted order, D, none of the above. All right. Third question, sorting as a computation technique is useful for A, searching an element in an array, B, finding the maximum or minimum element in the array, C, adding up elements in an array, D, finding the sum of squares of elements in an array. Simple questions, three simple questions. Everybody finished? So, can we have the quiz sheets collected? So, now we shall discuss a practice problem. This is a fall loop. Consider the falling loop in a selection sort, which we have just seen. For current top equal to 0, current top less than n, current top plus plus, maximum index, current maximum index equal to find index of max, array A which is past, current top and n. After this is called, swap function is there, which A, current top and current max index as the parameters. You have to identify as many places in the loop where the redundant computation can be eliminated. That means that there are certain computations, which you do much more, I mean you do it, but it is not required. There are some computations over here that you need, that can be eliminated. Without that also, selection sort will work perfectly. So, I will give you five minutes to think over it, write it in your notebook, then we will discuss it. Has everybody understood the question itself? No, okay. So, here is a very simple example. There are three numbers, one, one and one. You do not have to find the maximum out of the three. Okay? Suppose you have, you have found out the maximum and if you still start comparing again, you have found out the maximum which is one. But after reaching this index, again you are comparing. So, that becomes an unnecessary loop. So, something similar to that, I mean this is not the way which is there in the program. But something, some extra steps you are performing in this program which is given, you have to identify that one. Okay? Okay, redundant computation to find the maximum index. Okay? Okay, start discussing amongst yourselves. T s, can you go around? So, in general, what is the feedback that I have received is, that if current top equal to 0, current top less than n. So, do you all agree with less than n or something else? Less than n minus 1, right? And current top, why is it that you want to do n minus 1? Any one person? Right? It is unnecessary to check for the last element because you have already sorted the numbers in descending order or ascending. Now, current max index equal to fine index of max. Is this redundant? Do we need to do this step? Yes or no? Yes. Okay. Now, swap A, current top, current max index. Is this required? Okay. Now, there was some other suggestions that suppose if value is 24, 32 and say 12. Okay? Now, we find the maximum. So, this gets swapped 32, 24 and 12. Now, suppose we find that this is greater than that. We need not go to the swap function at all. Everybody agree with that? Yes. So, swap in certain cases is also redundant. But if you go to the efficiency of the program, you are actually computing some other extra step also. But you are avoiding the function called to the function swap. Yes? Okay. So, these are the two main things in this function, redundant in this function. Right? Any more answers? Shall we move to the next problem? Just posing the problem, you can think over it. Okay? Here is an array, unsorted array A containing n elements. Here, there are 12 elements I suppose. Yeah. Now, can we adapt or modify the selection sort so that we can save some computation compared to the selection sort and A is divided into three sorted segments, not necessarily of same length. Okay? So, if what the answer expects is for example, three segments of it can vary of different length. So, out of this, 0, 3, 9 is sorted, which is the first segment. Agreed? Second segment is 1, 6, 7, 8, which is also sorted. Not necessary that it should be in ascending order. Here it should only that particular segment should be in ascending order. Over here, the third segment is also in ascending order. Okay? Can we achieve this? Yes, the answer is yes. So, you need to think about it. Basically, what you are supposed to do is you have to divide it into three segments and you have to make sure that only in that segment, the elements are sorted, not the entire segment, not all the three segments. Everybody has understood the question. Okay? Think for some time and then discuss with the partner. Okay? How do we define? No, so, you are given that there are three segments. If you achieve this, then we can say, okay, now there are n-1 segments, n-2 segments or one segment each. Right now, consider that there are three segments and you have to sort the elements amongst the three segments only. Yes, so, 0, 3, 9, 1, 6, 7, they are just, this is just one sample. You can have something else also over here. You can have this 1, 6, 7, you can have your 0, 3, 8, 9. So, that is okay. Just we have to, this is just an example. Okay? Yes, everybody? Okay, some answers? Okay, I did not move around much. I was discussing the previous problem with some people. Okay, any solution? Anybody would like to tell? Since it's already time up, we don't want to waste much time. I still see some people discussing, having some nice discussion there. Can anybody share any one of, any one solution? Okay, sort of. So, one answer that we got is, that iterate the loop, three loops, 0 to 3, 0 to 2, then next one and then next one and then do sorting on each one. Okay, is that a good solution? No, no. So, who said no? Okay, one person is pointing out. Yes, would you like to discuss? Okay, any other solution? Yeah, that is also okay, but it's, I mean, over here, I mean, that is not, I mean, for before sorting, you need to divide it and then sort.