 Hello everyone. So in this video, we'll talk about our second Big Oven squared sort, which is selection sort and I like selection sort for the simple reason that it's probably the easiest one to understand at least from my point of view. So the way that selection sort works is that you are ordering the values in your list by repeatedly putting the largest value into its final position, right? So what you are selecting is let me find the biggest thing in this list and put it where it needs to go, which is probably the end of the list. And then once I found the first biggest thing, next I'm going to find the second biggest thing. So each pass through the list places the next largest value in its proper place, which is kind of like bubble sort, right? But it has the important property where it only makes one exchange or every one swap for every pass through the list, right? So that can be really important. Selection sort is not a stable sort. So maybe the the easiest way to think about selection sort is it's a combination of searching like a linear search and then with a swap at the end, okay? But you got to do it for a number of times equal to however big the list is, okay? So we're going to do selection sort and where we what our algorithm is going to look for the largest value, okay? So the way it works is we're going to start at the beginning of the list at index zero and we're just going to keep looking for the largest value, the largest value. And once we know that we found the largest value because we've looked through the whole list, then we're going to swap that largest value into the last slot because that's where you want to keep the largest value, all right? So the slot that we want to fill is slot five. This guy here currently 50 is in that slot but we're looking to put the biggest item here, okay? Now in order to do that, what we will do is we will search through this list linear search one at a time, one, two, three, four, five, six comparisons, trying to figure out who is the largest, all right? Well, as we go through this first time, it should be pretty obvious that 84 is the largest, right? So we'll keep track of as we go through which slot has the largest value, okay? So in this case, it's slot three, zero, one, two, three, then when we reach, we've found the largest, this is the first pass, we're going to swap these two values, okay? So now our target to fill is this slot right here, slot number four. So what we need to do is find the largest value in slot zero through slot four because this slot is filled, it's done, okay? Well, the largest value in here is what? It's 67, which is in slot zero, and so at the end we swap 67 with 49, okay? And on and on, right? In the case of 50, 50 is the largest value in this iteration. We're trying to find the largest value in this kind of sublist. So 50 is the largest value. We actually swap it with itself, but it doesn't move, it just stays here, okay? So this is the intuition. Basically, you search through, find the biggest thing, put it in the end slot, search through this part of the list, find the biggest thing, move it to this slot, and on and on and on, okay? So in the end, the algorithm winds up in the sorted list. But again, the key here is that unlike bubble sort, we are not swapping a lot. We only swap one time, okay? So there's a couple of visualization links here. Please check them out. Just a warning, these two are instead of searching for the biggest item in the list, they search for the smallest item and put it at the beginning. Same premise, just kind of iterate a little differently. So again, selection sort has the property of minimizing the number of swaps, and this can be really important, really useful if the cost of swapping items is high. So when is the cost of swapping items high? Well, part of it depends on where those items are. If they're located on the network, like maybe they're on some remote disc somewhere, transferring data across the network can be slow. Or maybe you're swapping or sorting very large items, like maybe you're sorting videos that you have downloaded by their running time, right? And so if you need to move the video files in some way, that can be a very expensive operation, right? So sometimes it's very useful to minimize the number of swaps. Let's walk through the algorithm a little bit. So I am abandoning writing on my tablet because that was really annoying and painful and we don't all need to sit through that again. Try and get this arranged on my screen in kind of a good way. Okay, so let's look at this algorithm. Here are the details of it, and there we go. All right, so we want to fill the last position and work backward, right? So our list here is A, right? Our A is our list of objects, and this is its initial state. We've got a list with... So it kind of looks like this. 12, we could say A gets 12, 6, 22, 14, 8, 17, right? So this is our list when we start out, and we're going to fill from the last position and work backward. We're going to put the largest item in the last position, figure out what that is, and then fill in backward. What's the largest? The next largest? Second to next largest, etc, etc, etc. Okay? So let's walk through the algorithm here. This is our starting state. For fill slot, gets n minus 1 to 1. Okay? So fill slot gets n minus 1. n is always the number of elements in the list. So there are six elements in this list. So fill slot gets n minus 1. This must be 5. Okay? Position of max gets 0. So position of max is going to be our variable where as we search through the list to find the biggest, we keep track of the slot that holds the biggest. Okay? So it starts out that the position of the max is 0. Okay? By definition. All right. Now J, for J gets 1 to the fill slot of 5. What are we going to do? All right? We're going to compare if A at the position of the max. So position of max is 0. So we're talking about A sub 0, which has the value 12, is less than A sub... Oh, I'm sorry. We're starting at J, right? So J is here. If A sub position of max, A sub 0 is 12, is less than A sub J. Well, 12 is not less than 6. Okay? So nothing happens, right? This loop does not fire because 6 is not less than 12, right? So we did do a comparison here, but we haven't swapped yet. Okay? All right. So now this for loop terminate, or excuse me, this if block is done, we go back to the inner for loop here. J is 1 to fill slot. So we're on to the next row. So J is 2. If the position of the max, which is still 0 here, right? We didn't change position of the max. If the A sub position of the max, A sub 12, is less than A sub J. J is 2. A sub J is 2 is 22. Position of the max is less than A sub J. So we need to update the position of the max to being J. Position of the max gets J, right? So what we're saying is that 22 is now the biggest guide that we have seen. Perfect. That's actually correct, right? All right, we continue on. So J is now 3, right? We compare 14 to the position of the value in the position of the max is 14 less than 22, or excuse me, is 22 less than 14? No, it's not. So the max is still in position 2. We've done another comparison. All right, moving on. We're doing the inner loop here, right? We're doing the inner loop here. J is now 4, all right? If A in the position of the max, which is 2, is 22 less than 8. No, not currently. All right, so we don't do anything. We've done one more comparison. Position of the max is still 2. And we increment J, okay? So this is going to be the last iteration, right? Because we're going from 1 to the fill slot, 2 and including. Fill slot is 5. All right, is the position of the max slot 2 is 22 less than A sub J 17? No. All right, so here we are. We've ended our comparisons. The position of the max we have determined is still 2. All right, what do we do? We have exited this part of the loop now. We've gone through all the J's. Now we swap the values. A sub fill slot and A position of max. Remember our fill slot is 5. Still 5. We haven't changed it this whole time. It's still 5. So we swap the fill slot, which is 5, with the value at the position of the max, which is 22. So 22 goes here, and 17 goes here, right? We swap these two right here, okay? That's all. Now the remaining values are still there. We're still working in memory. We haven't changed them, but the ones that changed are these guys. Is that in this way? Oh, I know what I need to do is I'm shading. Let's do it this way. So we've swapped these two guys. And all told, we did do one swap here at the very end, and we swapped one time. All right, so now what? Well, 22 is in its final resting place, right? So we're never going to touch 22 again, actually, because now we've exited the first iteration of this outer loop, and we've come back around. Okay, the outer loop goes from n minus 1 down to 1. So last time it was 5, we're counting down. Now it's going to be 4, right? So we are no longer going to touch this slot. It is fixed, right? I can just put 22 here forever. Okay, nothing's going to change. All right, so let's go through it again. Position of max, right? We just did this line. We decremented fill slot by 1. Position of max gets 0. It starts out. 4j gets 1 to the fill slot. Okay, my fill slot is now 4. So is the position of the max, this guy, a sub 0, 12, less than a sub j. Is 12 less than 6? Nope, sure isn't. Okay, so we've done a comparison, but we did not swap anything, and we didn't update the position of the max. All right, moving on. j is 2. Position of the max is 12 less than a sub j, 22. Yes, yes it is. 12 is less than 22, or excuse me, 12 is less than 17. So the position of the max here is updated to j. Okay, so it's 2 again, but we still haven't moved it. All right, let's decrement our counter. 17 is, our counter is now 3, so we're comparing 14 to 17. 17 is not less than 14, so nothing changes. Go up again. Now we're comparing 4 to 17, which is a sub position of max. 8 or 17 is not less than 8. Okay, so again, once again, once we terminate this inner loop here, we've gone through all of our j's. Slot 2 now is still the position of the max. It has, the biggest one is now here again. Okay, so what do we do? We swap the fill slot with the position of the max. Well, our fill slot is 4. All right, we're trying to fill in fill slot number 4. So that's this guy. So we got to swap the value 17, which was here, with the value that used to be here, which is 8. So the other items haven't moved. All right, so we do a swap on our final step, one more comparison, one more swap, and we move on. Okay, so what's the pattern here? I'll just kind of fill this in really quickly, but hopefully you've got enough to trace it through. The fill slot goes down to 3. We start over looking at the first item. Go to j. Is j our fill slot? So 17 is now fixed as well. We're not going to change 17 anymore. It's stuck in there forever. Our fill slot is now slot 3 is what we're targeting. Okay, so is the item in the position of the max, a sub zero, a sub position of max, is 12 less than a sub one. Nope. Nope. All right, so they don't change. Next step, j is 2. Right, is the item in slot 2, which is 8 now, less than 12. Nope, 8 is not less than 12. All right, so the position of the max does not change. Increment again. We did do another comparison. Increment again to j. j is 3. Is a in the position of the max, 12 less than 3. Yes, of course, 12 is less than, or excuse me, 12 is less than 14. Okay, so the new max is in position j, which is 3. Right, and that's the end of our inner loop. We've hit, gone from j up to the fill slot, which is 3. Okay, so we've done another comparison. We swap now, and what we're going to do is we're going to swap the value in the fill slot, which was 14, with itself 14. Okay, so, you know, our algorithm is kind of silly. It's a self swap. It still counts as a swap, though. Right, it's moving it. You could write this algorithm a little smarter so that if these values are equal, you don't need to swap it. Right. All right, finishing this out. We're down to fill slot. We're trying to fill in slot number two. We filled in slot number three. It's not going to change. All right, j starts again at one. All right. So we ask our question, is a sub one less, or is a sub max less than a sub one? No, not again. 12 is still bigger than six. Okay, increment this is a sub fill slot, which is, or excuse me, a sub position of max, which is 12, less than a sub j. Yes. Oh, I'm sorry. I'm looking in the wrong spot, right? A sub j is eight. So the answer is no, no. So the position of the max is still zero. The position of the max does not change here, because the max is this guy, right? It's right here. All right, so final step. What do we got to do? We got to swap the fill slot with the position of the max. The fill slot is two. The position of the max is zero. So we're going to swap eight goes here, and 12 goes here. So these are the two that get swapped. Six is still stuck here. All right, one out 12 is in its final resting spot. Okay. All right, final thing. We got to fill this slot right here. Slot number one. Last one. Position of the max is zero. J is one. So is the item, looking at this list now, is the item in the position of the max is eight less than a sub j? No, it is not. So what do we do? Well, yep, they get swapped. Okay, so we are going to swap these bad boys right here. Six and eight, excuse me, the position of the max is zero. These two guys get swapped, and we are finished. All right, so this is our ending state right here. Six, eight, 12, 14, 17, and 22. All right, how many swaps have we done? Let me just make these all red. All right, how many swaps have we done? One, two, three, four, five. That's it. All right, how many comparisons did we do? Well, if you tally these up, we got five, four, three, two, one. That once again is 15. Okay, so this is selection sort. Again, I think it makes sense intuitively. You're finding the biggest one, moving it to the end, finding the next biggest one, move it to the end. It could be a little jumbled when you're looking at the algorithm, but that's the intuition, right? And what I really want you to have is that intuition. A few questions about it. I've made a word very unhappy. Me and Microsoft products are just not getting along today. All right, so in general, how many comparisons must be made for a list of length one? Well, this is the same as with bubble sort. And the answer is given by a formula n times n minus one, all divided by two. Okay. So once again, what is the big O of selection sort? Well, you can kind of see it here. The number of swaps is minimized. Number of swaps is order n, right? It's by the size of the list. But the number of comparisons is this given by this, which is n square. So selection sort is big O of, all right. Besides the list A. So highlight these for you all. Besides the list A, how much additional space variables are needed to run the sort? So we've got a couple of extra variables. We've got fill slot, j position of the max, and a temporary variable to store the swapped value, right? So not a lot of extra space. We didn't really address this in bubble sort, but we will see once we get into some of our fancier sorts that you do need some extra storage in certain cases. And so the size of the data you're sorting can be a problem. Can you perform selection sort on a Python list? Sure. Yes, of course you can. How about a linked list? Sure you can do it on a Python list. Code looks like this. No problem. For a linked list, yeah, you can. But for a linked list, yes. But it would be easier if you looked, if you filled it from smallest to largest instead of the other way around. We're filling the largest to smallest. In other words, you look for the minimum. Why is that? Well, where are you going to put the last item in the list? At the tail or the second to last thing, right? So with a linked list, this becomes a little trickier, I would say. What initial state of A would require the most swaps? Well, the most swaps would occur. We did do a swap here in every stage. But that isn't necessarily the case, right? We counted this as a swap. And it is a swap. But it could be the case that maybe we don't want to count this as a swap, right? So what would require the most state of A would require the most swaps? Well, any state has swaps, right? It doesn't really matter. That's the nice thing about selection sort is that any state has the same number of swaps. And that's why we prefer it, okay? So that's it for selection sort. Again, have a good understanding of how it works at a conceptual level. And also understand, you know, is it a stable sort? What's its n squared? What are there any pros and cons to states of the data where the sort may not be as effective, okay? All right, next time we will talk about our last n squared sort, which is insertion sort.