Python Challenges: Different ways of sorting lists part 2

Transcript

English (Auto-generated)

everybody is doing very well. Um So in this code cast we're gonna be focusing on another sorting algorithm for list. Um and this one you probably have heard before. It's bubble sort. Um it's a very well known when it's known for being the simplest way of sorting algorithms and we're going to attempt to each day. So um here commented I just have kind of a list of instructions of the logic of bubble sort basically. So basically while needing to swap to swap loop through the following and we're gonna read through the list and then we're gonna get current number and next number and if the current number is greater than the next number, we're gonna swap them and keep on doing that until the list is sorted. So what we're gonna do is absolutely put it below that. And what we're gonna do is while swap happening up here, we're gonna make it. So so what happened equals be able to start our well swap happen is equal to true up here. And then down here we're just gonna assume that no swaps have happened yet obviously. So we're gonna set it to false okay. And now what we want to do is read through the list. So let me just do that really quick. And what we're gonna do is four number and I don't want to just through the list, I want to get the indexes for this thing. So first four number In range in order to get the number of indexes range of the length of list but -1 because we don't that because we're gonna be also getting the next number. And and we don't want to get an air saying index out of range because we don't want to get out of range. Okay? So now that we got four Number in range land list -1. We're going to get the current number and next number. So we're just gonna name them current number next number. Current number is going to equal list and then the number that we're at the index that we're currently on. So it's gonna be number and then we're gonna do next number is equal to that same thing. But one over like next number basically. And and now that we identified identified current number and next number we're going to say it's a current number is greater than next number. That means that it's that's part it's definitely not sorted. So we're gonna swap them. What are we gonna do to swap them? You guys? We'll see. So we're gonna swap them. These current number and next number are now going to be equal opposite. So it's gonna be next number and current number. But since this actually doesn't change anything unless it just changes the actual are variable for a number. Next number we're gonna make a list and the number equal to r now current number which is actually next number. So confusing. If you explain it that way it's okay um list number plus one is not equal our next number. So there we go. And now since we did have to swap and d we are going to put swap happen equals troop and now it's going to continue on looping until it just stays false and now we're gonna do is print on list and up here we're just gonna make a list. The list is going to equal strength let's let's say (335) 462 31. Um I'm gonna do python three as always and then the name of our file and it works perfectly 1233456. And yeah so um I just wanted to show you guys that I wanted to do a process with you guys where first we have our logical solution made and then kind of put it in python's syntax in terms um Just so you guys could kind of see the process of doing it that way. Um So of course um you guys can try and re factor this as always. I feel like there are definitely things I can see here that might need re factoring and yeah I encourage you to try this and kind of try to understand how it works a little bit better because bubble sort seems easy and it is easy but I think actually knowing how it works is a step further that you guys can take anyway so thank you so much for joining this code cast and goodbye
16 Views 0 Likes 0 Comments

In this cast we will be making the very well-known sorting algorithm called Bubble sort. It is the simplest way of sorting lists so let's try it out!

Comment
Leave a comment (supports markdown format)