 Hello and welcome back. In this lecture, we are going to look at selection sort, one of the simplest sorting techniques. Here is a quick recap of relevant topics. We have studied basic programming constructs. In particular, we have looked at iteration constructs, the use of functions and the use of arrays and matrices among other things. We have also looked at what the sorting problem is about and we have seen some motivating examples for looking at the sorting problem. In this lecture, we are going to look at selection sort, which is one of the simplest and most intuitive sorting techniques and we will also see how we can program selection sort in C++. Let us quickly look at the quiz marks problem that we have seen in an earlier lecture. So, here we have the roll numbers of various students. They are marks in three different quizzes of CS 101 and in this column, we have the total marks of each student totaled across the three quizzes. Now, suppose we want to rank all the students in decreasing order of total marks. So, effectively we are only interested in this column of total marks and we want to sort the total marks in decreasing order. Let us say that as an additional simplification, we will say that if two marks are equal, for example, we have two copies of 24 here, then any ordering between them is okay in the final sorted order. So, we have seen how we might go ahead doing this. We can tag each marks by its position in the sorted order. So, since we are talking about a decreasing order, so 27 which is the maximum marks here would be at position 1, 25 would be at position 2, both 24s would have the position 3, but of course, we will have to put one of them at position 3, the other at position 4 and so on. So, once I have tagged every marks by its position in the sorted order, I could then simply rearrange the rows such that the marks with position 1 appears in the first row of the column, the marks with position 2 appears in the second row of the column and so on and this indeed gives our sorted column in decreasing order of marks. So, now the question is how do we go from this unsorted column of marks to this sorted column of marks. Now, for our discussion, I am going to treat this column of marks as an array which has 6 elements here. This will be the 0th element in the array and this will be the fifth element in the array. So, let us see how might go about sorting this in a systematic step-by-step way which can then be programmed on a computer. So, here is our unsorted part of the array. What we are going to do now is first we are only going to solve a small part of the problem, not the entire problem. Specifically, we will just find out the maximum element in this unsorted part of the array and we will just put that single maximum element at its rightful position in the overall sorted array. So, here 27 is the maximum element, I have put it at its rightful position which is the top of the overall sorted array. This leaves us with the remaining marks and in this particular case since I have moved 27 from here to the top of the array whatever was there at the top of the array earlier that is 24 that has been moved to the earlier position of 27 and now this is the remainder of the array that I still have to sort. So, this is now a similar, but simpler problem to solve. Why is it similar? Because I still have to sort this part of the array. Why is it simpler? Because I have to worry about one less marks. Now, how do I go about solving this similar, but simpler problem? Well in exactly the same way that we discussed just now. What we are going to do is we are going to solve part of the smaller sub problem. So, we will find out the maximum element in the smaller sub problem. Note that this may not be the maximum element in the original problem, but now we are trying to solve the sub problem. So, we find the maximum element in the sub problem put it in its rightful place in the overall sorted array which is the second position because the first position is already being occupied by 27 and then we put the remainder of the numbers here once again because I have moved 25 from here to this position. So, whatever was there in that position earlier that is 18 has been moved back here. And once again we have a similar, but simpler problem to solve which is to sort the remaining marks. And we could keep doing this we could say to sort the remaining marks let me find the maximum one. Here there are two possibilities I could have chosen this 24 or this 24. Let us say that I choose this 24 for the time being. So, then that comes to its rightful position I am left with the remaining marks and then once again how do I sort the remaining marks? Same technique I find out the maximum in the remaining marks put it in its rightful position in the overall sorted array and I am left with the remaining sub problem. In this remaining sub problem again I find out the maximum marks put it in its rightful position in the overall sorted array and I am left with the remaining sub problem which now has exactly one marks and therefore I now have my entire sorted array. So, this step by step transformation of this unsorted array to this sorted array is what is called selection sort. If you would have noticed at every step we selected hence the name selection we selected the maximum element in the current sub problem moved it to its rightful position in the sorted array and then went ahead to solve a simpler but smaller and similar problem. Now if I ask you to write a C++ program for selection sort here is how the problem specification might look like we are given an array A of n integers we want to sort them in decreasing order which means I want a 0 to be greater than equal to a 1 to be greater than equal to a 2 all the way up to a n minus 1 and of course our simplifying assumption earlier was if two elements are equal either of them may be ordered before the other. However note that once our program is written it is basically a fixed set of instructions for the computer to execute. So, once the program is written and if you give an array A of n integers it will finally sort it and it will determine what the ordering between equal elements is going to be. So, the point to note is that although in the specification we said that if two elements are equal either of them may be ordered before the other once the program is written everything is completely deterministic the program will determine what the final ordering among equal elements is going to be and we will briefly look at this in a few slides from now. So, here is how our program for selection sort might look like we have the main function here and in the main function I have this integer variable n which I am using to store the number of integers that the user wants us to sort. So, I ask for the number of integers read its value in n and let us say that in this program we are going to sort only an array of up to 100 integers. So, if the number of integers to sort is greater than 100 or less than or equal to 0 I output an appropriate error message and then I return minus 1. Note I am returning minus 1 from the main function the main function can return any integer value as opposed to returning 0 which is the usual thing that we did for most of our main functions in our programs now what does this do this basically tells the caller of the main function in this case the operating system that minus 1 has been returned by main. So, this is kind of like an exit code for the main function when the main function is returning back to the operating system it is telling that I am returning minus 1 indicating that something unexpected has happened if the main function returned 0 it means that everything was okay. So, you can basically use the return value of the main function to indicate to the caller in this case the operating system whether something unexpected happened or everything was expected. After we have done this input validation we are going to read the actual integers that we are required to sort. So, I have used an array a of size 100 to store the integers and this is another integer variable count that I am going to use to read in the actual integers to sort. So, I asked the user to give the integers to sort how many of our integers the user said he or she is going to provide and then the simple for loop I read in all of the integers that the user provides in the array a. Note that in this case the elements will be stored in a 0 through a n minus 1. After having read in the integers I have to do my selection sort what do I do here I say well let me assume that a current top through a n minus 1 is the unsorted part of the array. So, I am using an integer variable current top which will initially be initialized to 0 because initially a 0 through a n minus 1 is the unsorted part of the array and then within this loop all I am going to do is I am going to select the maximum element in the current unsorted part of the array and let its index be current max index and I will simply swap a current top and a current max index and then I will increment current top and then just repeat this entire procedure again. So, you can see this is exactly how we really sorted the column of total marks using selection sort. Current top to n minus 1 is the unsorted part of the array and current tops keeps decreasing until we actually reach n minus 1 in which case there is just one element left. Now as a C++ program we can implement this by using these two functions. So, I have said current max index is assigned there is a function find index of max it is going to find the index of the maximum element in the part of the array a between current top and n minus 1 and once it is found that index it is going to swap a current top with a current max index. So, let us see how this function might look like. So, here is our contract view of the function I want this function find index of max which takes an integer array and integer starting index and an integer ending index and our precondition is that start should be less than n and both of these should be within the array bounds of a and at the end of this function the post condition I want to guarantee is that the current max index that I compute should be such that a current max index is at least as large as all the elements in the array a from start through n minus 1 and of course I do not want any change in the array a I just want the current max index. So, note that I am passing an integer array here as a parameter when I put these square brackets here I am telling the compiler that I am passing an array as a parameter and this is not called by value within this function when I refer to the array a I will be basically referring to the same array as the caller function used when this function was called. So, here is how the body of find index of max might look like it is basically very simple we iterate through the elements of the array from start through n minus 1 and at any point of time if I find an element of the array which is greater than equal to the currently maximum seen element the currently maximum seen element is a of start to begin with if I find an element which is greater than equal to the currently maximum seen element I simply update the current max index to the current index and once this entire loop is done I am going to return current max index. So, this is fairly straightforward how would the function swap look like once again this would be the precondition that index 1 and index 2 the two indices I am passing to swap are within the array bounds of a and the post condition is that a index 1 and a index 2 should be swapped and of course this changes the array once again note that I am passing an array as a parameter I have put these two square brackets. So, this is not called by value within this function when I refer to a I am referring to exactly the same array as the caller function used when calling the function swap. So, here is how the body of swap might look like it is a very simple swapping program I use a temporary variable to temporarily store the value of a index 1 then I copy the value of a index 2 to a index 1 and then I copy the value of the temporary variable which was storing the original value of a index 1 to a index 2 and finally I return note that swap does not return any value. So, its return type is void. Now, having seen the selection sort function in C++ let us quickly see what the role of this comparison operator that we used greater than equal to what is its role in the entire operation of selection sort. Now, if I use greater than equal to let us see what might happen. So, here is our array containing a 0 through a 5 and let us say currently we are looking at only this part of the array. So, current top is a 2 the total number of elements is 6. Now, when I call find index of max with the array a current top as 2 and total number of elements 6 what happens is current max index is initialized to 2 start is of course 2 this is how the function is called and the counter i is also initialized to 2 and after that since I am using greater than equal to and since 18 is greater than equal to 17 current max would be incremented to 3 once again 24 is greater than equal to 18. So, current max index would be incremented to 4 and finally since 24 is greater than equal to 24 current max index would be incremented to 5 and therefore this is the element that will be swapped with current top. On the other hand if I used greater than here what would happen I would start here at the same position and then since 18 is greater than 17 current max index would increment since 24 is greater than 18 current max index would increment again, but this 24 is equal to 24 not greater than 24. So, current max index would not increment and therefore this element will be swapped with current top. Of course, what would happen if I use less than or equal to it is easy to understand a current max index would be at least as small as all elements in a start to a n minus 1. So, perhaps the right variable names to use here would be a current min index it is important to give meaningful variable names. So, what you see is that the choice of the comparison operator determines what the sorting order is and also how the equal elements are going to be ordered. So, in summary we looked at selection sort we looked at the intuition behind this technique we looked at a C plus plus implementation and we looked at the effects of various comparison operators in this implementation. Thank you.