 Hey 231 students, so we're going to introduce you to your first sorting algorithm in this video But first I want to pose to you the question. How would you sort of list? And so this is a brain exercise Given the following Python list devise an algorithm to sort it now You can't use the build-in sorting methods. We talked about last time, but think about it, right? It's it's a good exercise If I gave you this list and I said here's a quiz I want you to sort the list How would you do it? You're gonna need to either have a loop or use recursion Either one will be fine, but you are gonna have to loop over this maybe more than one time so I encourage you take a few minutes and just think about how would you do this, okay, and I'll pause here for a minute and I would like you to pause and think about this. How could you possibly do this? okay, so Hopefully you've taken a minute to think about it Your answer may be something that looks like one of the next two sorts We are gonna study which is bubble sort. Okay, so bubble sort is Well, frankly, it's not a terribly Practical sorting algorithm because there's lots of ways to do it better, but it's Often taught as maybe one of the easiest to understand. Okay, so the way that bubble sort works is It makes first of all, it has to go through the list multiple times and most sorts do They're gonna have to go through the list many times. So you're gonna have like a nested loop probably yuck But the idea the intuition of bubble sort is that on each pass through the list you are going to compare adjacent items and exchange items that are out of their natural ordering. Okay, so The effect is that each time you go through the bubbles the list and again You have to go through multiple times. You're gonna place the next largest value where it should be Okay, so you this is where bubble sort gets its name Each pass through the list on the first pass you bubble up The largest element to its final spot and then in the second pass through bubble sort You bubble up the second highest value to its final spot, right? And those spots are gonna be at the end of the list Bubble sort is a Able sort and so that means that if you've got two items in the list in the same order We'll show you an example in a minute. They're gonna maintain their order in the list Okay, I'll show you an example of what that means in in our worksheet, but At least I think I am but basically right if you had if you imagine that you've got a pile a big List of say coins in front of you. You've got pennies and nickels and quarters, right? What else dimes 50 cent pieces doesn't really matter those things have an ordering by their monetary value So imagine that they're all laid out in a row get out some coins if you have them lay them out in a row Stable sort means that let's say you've got two quarters in your list and you're sorting the whole list those two quarters When the sort finishes will be in the same order that they are on the table in front of you All right So when you start out the quarters may have pennies and nickels and dimes in between them But when the sort finishes Quarter number one will be first in the sorted list and followed by quarter number two And then if there was quarter number three quarter number four, that's what it means to be stable The ordering is preserved for equal values Okay, and that can be important for things like priority queues to say you've got people in a hospital and Some of them are patients who are designated as critical, right? Well, you want to see the critical pace patients before the ones who are Normal condition or not unhealthy whatever but Within those critical groups of people you want to see them in the order. They arrived That's effectively what a stable sort means people with equal value main are in the same order that they were in the list Not all sorts are stable sorts. Okay, so bubble sort What does it do? Let's visualize it just very quickly and I'm gonna get rid of myself Because I'll block some of the code. So here's a list. What does bubble sort do? So we're gonna start it starts Here at index zero of our list and basically what we're gonna do is we're gonna compare this item and Bubble it up. We're gonna move it up up up until we find something bigger and remember our end goal after we go through This list one time is that the biggest item in the list is In its final spot So just glancing at this you can see well 84 is the biggest item our end state our end goal for the first Pass through this list is gonna be to have 84 over here Now we can see this but of course the computer isn't when the algorithm is running It doesn't know the state of the list. It has to loop through to figure these things out Okay, so what does bubble sort do? We start with 67 and we compare 67 to 33 and we're comparing adjacent items And it asks the question is 33 less than 67. Oh, yes So we're gonna bubble 67 up a slot right so we swap these two elements we bubble 67 up And then we ask is 21 less than 67. Well, yes it is Okay, so we bubble up 67 another slot and you know that means we swap these two Is 67 less than eight or excuse me is 84 less than 67. No, it is not So on this comparison, we're still doing a comparison, but we do not have any movements here All right, so the efficiency of our algorithms and sorting we care about the comparisons and the moves Okay, there's gonna be no move here. All right. All right now. We're looking at 84. So we've gone slot Zero slot one slot two slot three Okay, so we ask is the thing in slot four less than the thing in slot three the answer is yes So we bubble up 84 again and then finally is the thing in slot number five Zero one two three four five. Yep. This thing in slot five less than the thing in slot four the answer is yes All right, so we bubbled up. All right So this is one pass through the sorting algorithm So we had to go through all the whole list and after doing it We know that the item that is in this final slot is the biggest one, right? Now we need to go through this list one two three four more times to kind of bubble up the next biggest things Okay, so after the first pass the largest element has bubbled up Let me turn myself back on here to the end of the list And then for each pass we will put the next largest number in its place, right? Some of the other elements may bubble up as well If you remember 67 started out here and it bubbled up part way All right, and then an encountered 84 and 84 bubbled up So after we've done this n minus one times where n is the number of items in the list the list will be sorted Okay I have some links here to some visualizations I'm gonna pull one of them over here so you can see it This is the the second one here, and it's pretty cool. It's pretty intuitive. I encourage you to look at it The code over here is written in Java. So don't worry about that. You don't have to concern yourself There's a bubble sort handout. We're gonna go through the algorithm in detail In the second part of this video, but I want you to just kind of look at this until you get a good Feel for what's going on, you know, don't pay attention to the code pay attention to this part over here You can control the speed of what's going on Let me restart it And play it you can actually see pause restart pause, right? It starts out by comparing nine here to 86. Well nine is less than 86. So it doesn't move it Um, and then 86 is getting compared to each thing and 86 starts bubbling up right And again every time we're asking the question is the item in slot j plus one less than The item in j right keeps bubbling bubbling bubbling turns out 86 is the biggest element of this list So it's going to bubble all the way up to the top and then we start over Okay, so now we're kind of As you can see things are starting to bubble up again. We're always looking for We're doing comparisons like is the thing in slot j Less than the thing or excuse me is the thing in slot j plus one less than the thing in slot j Right. So here's bubble sort. It keeps bubbling up things start to kind of get into an order here as well Right to the left hand side. All right. So kind of watch this try and understand what's going on Second part of the video. We're going to turn to the worksheet and really get into the algorithm Hello, everybody. So this is the second part of the bubble sort video This one we're going to walk through the worksheet that I've posted online so I've also posted the solutions but You know as good students you should make a good honest effort At these worksheets first The worksheets are designed to first answer a couple of questions about the sorting algorithm Like how efficient isn't what it's good at or bad at but also to show you the algorithms in detail Okay, so we're going to start out with bubble sort Which I introduced in the first part of the video So on the screen down here, you can see the Algorithm for bubble sort is pasted here. Now. This is in pseudo code It's not actual python code if you want to translate this into python. Hopefully there's enough information here so We'll step through this. I've got hopefully some english language description over here of what each line is doing But let's just start it. Let's try and do it And maybe that's the best way to kind of explain how all this stuff works. Okay So I've got a list here. I've got a list a That I want to sort and let me make sure I'm ready to draw looks good I've got a list here that I want to sort as you can see it's a list of numbers Numbers have a natural ordering. We know what less than means in number world So we have the ability to sort this list First thing I'm going to observe though is how many items are in the list. What is my n? What is the size of the input? Well, I count uh that there are six items in this list So I'll write down n equals six here All right, so now let's trace the algorithm Right, so the algorithm starts out as you can see we've got two four loops here We've got nested four loops and that should give you a hint as to the algorithmic time complexity of this Or the big O of this algorithm and anyway, let's just trace it. Let's do what the algorithm says Four passes remaining so passes remaining is a variable goes from n minus one down to one Okay. Well when we start out n minus one to one, so we're looping backwards We're kind of counting backwards here. We start out n is equal to six so passes remaining here in our first Go through is going to start out at five pat n minus one is five all right, so Now our inner loop passes remaining as our control variable for the outer loop Our inner loop is the part that bubbles each time. It's going to bubble the biggest item up That's its job. All right, so for j equals zero j gets zero to passes remaining Minus one. Okay, so passes remaining minus one, but we start out with j equals zero Okay, if A sub j plus one is less than a sub j We're going to swap them. Okay, so j is zero. So we're looking at this element right here, right? We're looking at number 12 is j plus one Okay, so j is here right now, right slot zero is j plus one this guy Right slot one less than A sub j. Why yes, it is right a sub j So then we need to swap them. Okay, so i'm going to write down that we have swapped These two elements right and i'm going to make a note over here. Yeah, we swap them Okay, it'll be useful for us later Uh, and we did one comparison here, right? We we also did a comparison on this line All right Now, uh, the remainder of this list remains unchanged at this point, right? We're just we're we're doing One pass one iteration through this inner loop nothing in the rest of this list has changed yet. Okay, so We've completed one pass through the inner loop now j will go on for j is starts at zero and goes up to passes remaining minus one So now j is going to be one Um, so we've moved our pointer and now we're looking Here. Okay, so we're not going to touch six six is still there j is one Is j plus one less than j a sub j. Well, no 22 is not less than 12 So No change is made. Okay, the list remains the same There is no swap here. We did do a comparison. We compared 12 and 22, but no swap in this one All right, so now we move on j is two All right j is now here All right, it's slot two Okay, so these guys were we're passing them already And we're looking at slot two is slot j plus one 14 less than slot j. Why yes it is so we got a swap Okay, so we've swapped these two values. We have performed a swap. I'm going to mark that We have performed a comparison And these guys we haven't gotten to them yet All right, so now what do we do j goes up again according to our algorithm. So now j is here Okay All right j is here. So is a sub j plus one which is eight less than a sub j. Why yes it is Swap these two around Okay, so we're only dealing with adjacent pairs. Whoops, excuse me We're only dealing with adjacent pairs Uh, we did do a swap here and we did a comparison Okay so finally Last iteration again, this is the iteration of the inner loop for j gets zero up through passes remaining minus one Well passes remaining has been five this whole time All right, so this is going to be the last iteration passes remaining minus one Um, and we're comparing four and five here, right? So I'm going to stop You know filling in all these things that we're not changing Um is 22 Less or 17 less than 22. Yes, it is. So we swap them There is a swap and there's a comparison Okay, so Take back what I just said. I will fill them in um This is the state of a this is what a looks like After the first complete Uh set of iterations for the inner loop. Okay, so the outer loop has completed one pass This is the current state of our list And during this pass we did one two three four swaps We did four swaps and we did five comparisons Okay, so let's uh Continue to fill out this table All right, so what do we do now? We've completed one Part pass of the outer loop So for passes remaining gets n minus one down to one So this passes remaining variable is decrementing. We're counting backward So we're gonna go I'm gonna get my mouse back here All right So now passes remaining goes to four My uh my tablet does not like On my side of my hand touches it very hard to write In that case, isn't it? Oh, no Now what have I done? I think I've moved all sorts of things inadvertently and let me try and get this back Hopefully that's okay now. Holy cow. What a pain All right, so j is now Uh j starts over again. So we're on the inner loop again for j equals zero to passes remaining minus one So now we start at zero again Okay, so we're here. Uh, let me erase These guys I'm gonna move slightly more quickly now. Okay, so it's six less than 12 or is 12 less than six No, so there's no movement on this row. Okay Okay Six 12 14 8 17 And 22 right, so I've shaded out 22 here 22. We're not going to move it anymore It's frozen remember each pass of the outer loop in the bubble sort Bubbles the biggest item to the top whatever that may be so 22 is there. It is set. Okay um in this first iteration We did not uh do We did not do a swap. We did not do a swap. Let's try Let's be careful. Okay. We did not do a swap. We did do a comparison though All right All right, so now we're on iteration number or where j equals one. So we're looking at j and j plus one so 14 and 12 is 14 less than 12 No, it's not so once again You know, we don't move anything Um, all right. So there there has been whoops there has been no swap Here we have done a comparison though All right, let's keep it up J is two. All right. So now we're looking at slot two and slot three Right is eight less than 14. Why yes it is So 14 is bubbling up right. We've bubbled up 14 here So we did this swap. The rest of the elements are currently unchanged. Right. We're only looking at j and j plus one And we did do a swap here. We also did a comparison All right, finally passes remaining for j gets zero to passes remaining minus one. Here we are. We're at three now um, and we're looking at this and four. All right is J plus one 17 less than j 14. No, okay, so What has happened? We haven't we haven't done a swap. We've done another comparison though But look at where we have arrived 17 Holy cow doesn't like me today 17 is in the next highest slot. Well, what's the next highest value in this list? That's remaining. Uh, well, yeah, it's 17. Right. So after the second pass of the outer loop 17 The next biggest item in the list is in its final resting place and there it's going to stay So we did four comparisons this time and only one swap All right Well, let's do our algorithm. You know, let's finish this out Okay, we're we're getting close here. So pass Passes remaining I apologize. It's more frustrating for me Goes down to three We start j over at zero. Okay Is six less than 12 or 12 less than six. No Okay So we've done no swap I'm gonna I'm gonna have to write an angry letter to microsoft. Do you think that they will care about that? I totally blame this on one note. Um Let's see. Can I hold my pen a different way? If I do it this way? All right Um, so we haven't moved anything j goes up to one Here. Okay. So we're looking at this slot All right is j plus one Slot two less than slot j. Why yes, it is eight is less than 12. So these items do get swapped around I did do a comparison and the rest of the items remain the same 14 All right, finally j goes up to two Right, so we're looking at two and three Is slot j plus one slot three less than 12? No So there's no movement here. Okay 17 No movement. We did do a comparison. So here again, we've got three comparisons In one swap. All right. Well, we're almost done Now you may be observing here if you're observant and you're understanding what's going on The list is actually sorted at this point All right, but unfortunately, uh, our bubble sort is not that smart So it's going to keep going. It doesn't know that things are sorted. So it keeps following its algorithm Passes remaining goes down to two j starts over again at zero Okay, so we're comparing slot zero to slot number one Okay, so zero to slot number one Okay, eight is not less than six. So we don't do a swap All right. So there was no swap. There was a comparison. The remaining items stay where they are J goes up to one All right, so we're comparing slot one and slot two Uh Slot one is slot two 12 less than eight. Nope. Sure isn't Okay, so no no swaps again Kill me here So no swaps But we did do Two comparisons All right. Finally, we're we're almost to the end. Thank goodness, right? This is an n squared algorithm and you feel every bit of pain while trying to do it, right? Um, finally passes remaining Goes down to one right and if we scroll up i'm Scared to scroll anymore If we scroll up we see that passes remaining goes from n minus one down to one So this is the last iteration of our order loop all right, so J starts at zero and then finally we got to wrap this up and we say We say This is awful. Uh, we say all right J is zero And j plus one is j plus one eight less than six. We'll know of course not so we're we stop Send help please All right. Well microsoft hates me and i hate them Feeling is mutual Heh can't even write i can't write without putting my hand down, but when i put my hand down it scrolls all right, so Uh, this is the last iteration of our algorithm. We did not do any swaps here, but we did do one comparison All right, so let's kind of tally everything that we've done here now our list is sorted, right? We've got six eight 12 14 17 and 22 All right, how many swaps did we do? Total well it kind of depends maybe you've gathered The number of comparisons let's talk about that first the number of comparisons depends on how big the list is So we've got five four three two one some all that up. That's 15 All right, we did 15 comparisons total and how many swaps did we do? Well, we did four initially So we bubbled a lot in that first pass and then one swap one swap So six total right so the the number of swaps you do Maybe you kind of picked up on it depends on the initial arrangement of this list If we tried to do bubble sort on a list that was already sorted we wouldn't swap anything, right? So The number of swaps depends on kind of the initial arrangement of the list Whereas the number of comparisons depends on the size of the list not its arrangement All right, so let's scroll down. We got a few questions to answer here So in general How many comparisons are needed for are made for a list of length and Well, you can kind of factor it around But the the correct answer is The correct answer is to stop using one The correct answer is n Okay times n minus one Over two, right? So we had how many items we had Did we have five or six items in that list? We had six items in that list So six times five is 30 divided by two is 15 Comparisons. Okay. What is the big O of bubble sort? Okay. We'll look at this. This is the worst case complexity here Well, that to me we've got n times n you've definitely got an n squared algorithm here Can you perform bubble sort on a python list on a linked list? Well, yeah, yeah, you can Looks basically the same for either version. So yes. Yes, of course you can We're starting sorting of the data structures. We already have We've only talked really about three data structures. We've talked about a lot of data types and abstract data types We've only talked about three data structures the array based list the linked list and the hash table Hash tables can't be sorted They can but we're not going to go there, right? You would use totally different algorithm in this Um linked list and array based list. Yes, you can both sort them with bubble sort Here's a thought Experiment what initial state of the list of a would require the most swaps? Okay, so think about that for a second. The answer is if you got a list That was already in reverse order So for some reason the list came in sorted backward and you tried to sort it with bubble sort You would be doing a lot of swaps, right? It would not be good Um, you would be doing n squared swaps on top of n squared comparisons. Um, or not n squared exactly with this value So this would be bad news, right? If we had to sort just reversed order things all the time Um, maybe a smarter approach would be just loop over the thing backward Uh in that case Final question. How can we short circuit bubble sort to detect if the list become sorted be going through all For going through all the passes Well, the answer here, let's observe at this On the outer loop here, right? When we went through pass two we did no swaps Right, and that's because at the end of pass number three our list was sorted at the end of pass number three of the outer loop So What we can say is, you know, hey if we go through a pass of the outer loop Without doing a swap if you haven't swapped anything it must be sorted Okay Pass of the outer loop. So how can we short circuit bubble sort to detect if the list is sorted before going through all the passes? the answer is um The list is sorted If no swaps Are performed Okay, so what would you do with this? Um You know, you can use a boolean right Figure out oh my goodness use a boolean to figure out. Hey, have I done any swaps in this pass of the outer loop? And then when you come back to your outer loop, you know loops around again You check that boolean and if it's like hey false. I didn't swap anything. You're done return exit out of there All right, so there's another table here I encourage you to to work through it try and concretize The basics of how this algorithm works. Okay, get it kind of in your head what it's doing Next time we'll look at a sort that operates in a totally different way It may be a more natural way as well. All right. See you then