 Welcome back, in the previous segment we saw how an array could be passed to a function. Now we are going to use those ideas to build a somewhat more elaborate function. So this function is going to sort an array, so the input is an array containing numbers. The output, well the output is going to be present in the same array and you can think of the goal as being to rearrange the numbers so that they appear in non-decreasing order. So as an example, suppose the array initially is 35, 12, 29, 70, 18, 29, then at the end we want this desired order. So 12, 18, 29, 29, 35, 70, okay, so how do we do this? Well before we say how do we do this, I should point out that this is an important operation and chapter 16 will give us a clue as to why this is the case. So we are going to do that, so we will just wait until then. There are many algorithms for sorting and chapter 16 will discuss a clever and fast algorithm. Here we discuss a slow but an easy to understand algorithm called selection sort. The basic idea of selection sort is find the largest number in the array. Then we exchange it with the element in the last position. Now we have made progress, why? Because the last position now contains the largest number which is really what we wanted, the last element of the array to indeed contain the largest and we have placed it there. So what happens now? We just have to do the same thing for the first n minus 1 elements. So we apply the same idea to the first n minus 1 elements of the array where n is the length of the array, then to first n minus 2 and so on. So a primitive step in all of this is finding the index of the largest element. Well we said we want the largest element but actually it is more useful to find the index of the largest element. Where is that largest element present in that array? So here is a function which does that. So this function takes as argument an array, well an array name but as you know it is the starting pointer to the start of the array and the length of the array. But we can think of a as an array name as we saw that when the parameter passing values are copied it really behaves like the name of the array. This function is going to return the index of the largest element in a. So what is the invariant for this iteration? Well so for the i-th iteration the invariant is that max index which will be a variable that we will define soon will equal the index of the largest element in a0 through i minus 1. Or I should say a index if there are multiple elements which are maximum it will be the index of one of those elements. So we start by setting i equal to 1 and max index equal to 0. So this makes the invariant hold why because if i equals 1 a0 through i minus 1 is just a0 and max index is indeed the index of the largest element. Well there is only one element a0 and its index is a fine index of the largest element in a kind of trivial but we will do. Now we are going to increase i until l minus 1 and as we increase i we want the invariant to hold. So this is like our max so far but instead of keeping track of the max we are keeping track of the index. So a of max index is max so far and if this is smaller than the new element then we are going to set max index equal to i. So either way max index will point to the largest element or be the index of the largest element and so at the end we return max index. I should note that in doing this the number of comparisons needed is going to be l minus 1. So for each value of i going from 1 to 1 to l minus 1 so l minus 1 comparisons will be needed. We are later on going to estimate the time taken for this and this value will be needed over there. So what is the main function and the main program? So the main function is the function selection sort. It again receives the name of the array which is the address of the 0th element but which we can pretend is the name of the array because the name of the array means the same thing and the length of the array. Then we are going to start with n. Here we said that we are going to place the largest element in the nth position then the second largest element in the n minus 1 position and so on so that is what this loop is for. We first calculate max index which is cos of max of ai. If you remember cos of max was going to return an index of the maximum element in the array a of length i. Now our array a actually has length n. The first iteration this will be n but in the subsequent iterations this will be smaller. But if you remember according to our discussion in the first iteration we wanted to find the largest element in the entire array. In the next iteration the largest element was already in position n. We wanted to find the largest element in the first n minus 1 positions and that is exactly what this statement will do because in the next iteration i will have come down one step so this will be n minus 1. So this is exactly what we want. So cos of max this function thinks that the array only has i elements but that is okay. So it is going to only tell us the index of the largest element in the first i elements that it is going to consider from this array. Now we want to do the exchange we want to exchange the i minus 1th element with max index okay. Why i minus 1th element well in the i-th iteration we are going to consider i elements okay. So this is element 0, 1, 2 and this last element is i minus 1. So we found some max index over here and we are going to exchange the element over here and the element over here. So what this will do is we already before starting would have had the correct values over here okay the largest values over here. Now the largest value in this entire region is going to be pushed to this point and so the good region will extend a little bit more that is what this is doing okay. So we need to do this exchange. So how is this exchange done well the exchange is simple enough we copy the value at max index to max value. So this is max index so this value goes into this variable called max value okay. Then we copy the value in i minus 1 into this position and finally we copy the value over here into this position okay. So the exchange is done okay. So that is it that is our selection sort okay. So in our main program we will create this array and then we will call make us call to sort okay. So we will print it but I will show that printing step when we do a demonstration okay alright. So before we do the demonstration let us review what we have done a little bit okay. So this is our function and let us say let us try to count how expensive how much time this whole thing takes okay. So we said that cos of max our function will perform l minus 1 comparisons where l is the length of the array pass to cos of max okay. So this is what I am talking about so this red part this red part will require me to do l comparisons l minus 1 comparisons okay if the array has size l. So if the array has size i or if this argument is i it will do i minus 1 comparisons the first iteration it will do n minus 1 comparisons the next iteration it will do n minus 2 comparisons n minus 3 comparisons and so on okay. So Celsot calls pass of max for l equals n n minus 1 2 so the number of comparisons it does is n plus n minus 1 all the way down to 2. So it is n plus 2 times n minus 1 upon 2 okay so just about n squared by 2 comparisons. So even if I just count the number of comparisons Celsot will do about n squared give or take a factor of 2 okay and n squared turns out to be not such a fast algorithm. You can do far fewer comparisons and we will see that a little bit later okay but anyway this algorithm will sort correctly and now we are going to see a demo in which that sorting is going to happen. So let us take a look. So this is our function okay so this is our code for pass max I have added a print statement because I would like to print the array at different point so we will see where this is going to be used okay. So let us just take a look at the Celsot function okay. So I have modified it a little bit and the modifications are stored shown by these stars. So what I am doing is at the very beginning at the beginning of each iteration I am going to print out the entire array. So this way you can see how the array is going to change and to make sure that okay so then this pass of max is going to be called and I am also going to tell you where that index is what index was reported. So again we can check how pass of max is working how the entire thing is working and I do not want the iterations to go off very fast okay so here I am going to just have a dummy variable okay and I am going to read a number into it I am not going to use this but this will just force C++ to stop at this point and wait for me to type in something so that I can continue. So the value of the dummy variable is not important over here I will just put in so that I can force C++ to stop okay. So the rest of the code is exactly the same and at the end also I am going to print out a message and I am going to print out the value of the array okay. So we had the print function at the top and that simply prints out the value of the array value of all the elements in the array. So let us execute compile and execute this. So let us run it okay so this is the print out at the beginning of the first iteration of cell sort and then it so this is at the time when nothing has been done we are just beginning the very first iteration and we have issued max pass and it says that the maximum value is found at index 3 let us check if that is true. So 0, 1, 2, 3, 70 is at index 3 and it is indeed the maximum value. So max pass has behaved correctly. So let us just type in some nonsense value so that the program will continue okay. So now you see that the second iteration is being executed and at this point 70 has already gone to the end because of our exchange and we have also issued the call to one more max pass and that is limited to this region and in this max pass is found at index 0 which is indeed true. So again let us continue okay and now 35 has gone to the end and now we are finding that max pass is found at index 2 0, 1, 2 yeah there are 2, 29s but it has picked one of those and so and it is indeed at this position there is indeed the maximum a maximum value. So let us again make it go forward so this time 29 has gone to the end well we do not know whether it was exchange or not but presumably there was an exchange but now again a maximum is found at this position okay. So let us again type 0 so it has gone forward and this time it is really searching within this region max pass is being found within this region. So the largest value is over here and so index 0 is returned by max pass and so at this sorry let me type 0 so at this point the function is returning and after returning to main program 12, 18, 29, 29, 35, 70 is printed and it is indeed correctly sorted alright. So POS of max will perform L minus 1 comparisons where L is the length of the array passed to POS of max okay. So we saw all these things okay we saw the demo and we can do a variation we can express sell sort as recursive program okay. So I am going to leave this I am going to show you the code but I am going to leave this as an exercise for you okay in fact I am going to put out the code as well and you can run it yourself and check it okay. So that essentially concludes this lecture and so I would like to make some remarks and in this lecture we saw what sort of happens behind the scenes when we access arrays okay and we said what does an array name mean and what happens for the indexing operator and how do you pass an array to a function okay and we said that when you pass an array to a function it does not copy values but it merely copies the address of the zeroth element and if you want to access the entire if you want the function to access the entire array you should tell the function how many elements the array has. So that also must be passed okay writing functions on arrays is useful and this is definitely a skill that you must master the selection sort runs in time proportional to the square of the number of elements being sorted and in chapter 16 we will discuss faster algorithms. I will stop here but I will urge you to solve the problems at the end of chapter 14 and of course read chapter 14. Thank you.