Python Tutorial 12: Simple Python Sorting Program

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter from toptechboy.com and we're here today with lesson number 12. in our incredible new tutorial series where you're gonna learn python or you're gonna die trying i'm gonna need you to pour yourself a nice strong cup of black coffee that would be straight up black coffee no sugar no sweeteners no creamer none needed because you got to remember you guys on this channel your jet fighter aircraft and what does a jet fighter need it needs jet fuel and your jet fuel it's not a coca-cola or a mountain dew it's straight up black coffee i am also going to need you to get out your visual studio code and as you are firing up visual studio code as always i want to give a shout out to you guys who are helping me out over at patreon it is your encouragement and your support that keeps this great content coming you guys that are not helping out yet look down in the description below there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough of this shameless self-promotion let's jump in and talk about what we are going to do today we are going to look at the solution to the homework assignment which i gave you in lesson number 11. and that was to take our grades program which i believe by the time we got through with lesson number 11 would input a set of grades would average the grades would find the high grade would find the low grade and then print the high low average and the set of grades okay what the assignment though was was to take that program and then sort the grades from high grade to low grade so you would assume in that list that the grades would be in random order but to sort the list of grades from the high grade to the low grade was anyone able to do this homework or did you all fold up like cheap walmart lawn chairs okay now i will say that if you use the python sort function that would be considered cheating because i need you to think i need you to learn how to think you don't learn how to think by just using someone's canned library you've got to think through problems like this because when you become an engineer and someone's paying you they're not going to pay you for a solution that you can just call a canned routine they're going to pay you to think through something and code it that's what i want you to do learn how to think like an engineer learn how to think like a programmer and write code on your own now if you were not able to do this particular homework assignment it's kind of understandable because this really i think it's a lot more subtle and a lot harder than what you think but it's kind of like a classic homework problem that you're going to be given in college and so you sort of need to be able to think through it and know how to do it so that's what we're going to do today we are going to look through the solution now really in sorting routines it's all about who can sort the most efficiently so like the the quickest most efficient implementation of sorting long list we're not going to worry about being elegant and efficient and fast i just want to sort through it as simply as possible so that you will understand it and then later on we could perhaps look at more efficient routines but it's just kind of like starting from scratch and building a program that would sort a list of grades and like i said there's a couple of gotchas in here that make it a little bit harder than you would think and so for this particular program we better map it out on paper and we better get it working on paper and if we can get an algorithm in our mind on paper then we can code it but this is not a program that you can just sit and start banging out you got to kind of think about it all right so we're going to go over here to sketchbook and i'm going to get out of your way and then we're going to kind of think through this and so let's see if i can come up here let's just create an array okay and so let's put four grades in our array and just to make it fun i'm gonna put it in the worst possible order right i wanted to sort from high grade to low grade and so let's say we have 4 grade 60 70 80 and 90. now for this case this is kind of like the worst possibility right because they are in exactly the wrong order well what do we want to do we want to step through this array and we want to compare we want to compare grades of i okay grades of i to what grades of i plus 1. so we're going to start with i at 0 and then we're going to look at grades of i which would be grades of 0 which would be 60 and we want to compare it to the next one in the list which would be grades of i plus 1. so the first time through the for loop i is 0 so it's going to be looking at 60 and it's going to be then comparing it to the next one in the list which would be grades of i plus 1 which is going to be 70. well what do you want to do well if grades of i is if grades of i is less than grades of i plus 1 as it is here then those are in the wrong order so we're going to have an if if grades of i is less than grades of i plus 1 then what we want to do is we want to swap their positions okay we want to swap their positions and so what this would be is you can see it's going to be 4 i in range so right we want to step through these numbers and then if two adjacent numbers are in the wrong order then we want to swap them so for i in range we want to go from 0 which is the first number to numb grades and we want to increment by one okay now this is the thing you have to think about though that i'm going to the first time through i'm going to compare 60 to 70 the second time through i'm going to compare that to that and then the third time through this to this what you got to see is if i'm comparing the present position to the next position do i really want to go through all the way through numgrade times no i don't because when i get to 90 it is going to try to compare 90 which will be at that point grades of i to grades of i plus 1 and there is nothing here so you are going to get what you are going to get an out of range index and so because we are comparing the present grade to the next grade we don't want to go to num grades we want to stop one before that because really the last thing we want to compare is the 80 to the 90. we don't want to go through that last time and so hopefully that makes sense and that's one of the things why these programs crash is because you think well i got four grades i need to go through the list four times no you need to go through three times because you're going to compare first to second second to third and then third to fourth and then you don't want to go through that last time and so i hope that makes sense because that is one of the things that makes this program not work and so let's see if we can clear this and let's see if we can come back let's start this baby again and so again we're going to do four grades and it's going to be 60 and it's going to be 70 and it's going to be 80 and it's going to be 90. and so what you can see is we want to go through one we want to stop one before num grade and so i would say 4. i in range and where do i want to go i want to start at 0 i'll start at the 60 and then i want to go num grades minus 1 because i don't want to get to the 90 and then try to compare it to a number that's not there and then i'll go in increments of 1. now what am i going to do i'm going to say if grades if grades of i that would be 60 if grades of i is less than grades of i want to compare 60 to 90 so i compare grades of i to for 60 to 70 i want to compare grades of i to the next one what is the next one in the list grades of i plus one okay and then we've got our colon and then we've got our colon all right so if this one is less than this one what do we want to do we want to swap them because you want the bigger numbers first and so then i would say if grades of i is less than grades of i plus 1 what would you do you want to swap those two now what you would probably think down here is you would probably think well inside this if statement i would want to say grades of i is equal to grades of i plus 1 and then grades of i plus 1 is equal to grades of i and so this is kind of like how you would usually think about it i'll take grades of i plus 1 the 70 and put it in the position of the 60 and then i will take grades of i and put in the position of grades of i plus 1 and in effect swap those positions but what you got to see is this does not work this does not work no that does not work why because let's think of this i'm going to take grades of i plus 1 and i'm going to put it in grades of i well then in that case 70 is going to go there and then 70 remains in the next position and then you would have 80 and 90. so you took grades of i plus 1 70 and you put it in grades of i now your list looks like this okay now when you say grades of i plus 1 that 70 is equal to grades of i then you're just going to put it back and you're going to have 70 70 80 and 90. so what happens there what happens is you lose the 60. and so what you got to see is is before you put grades of i plus 1 into grades of i once you do that you're going to lose your 60. so you got to take 60 and you got to put it off to the side so that you don't lose it all right so this is the way you have to do it and let's see if i can erase this oh my goodness i need a bigger eraser give me just a second here that's going to take forever let me get a big fat eraser all right that will erase it quickly okay so now let's come back to our most excellent pin okay so now what you have to see is is that what you're going to put in this if statement what you're going to put in this if statement is if you need to swap the first thing you need to do is you need to put that 60 to the side so swap is going to be equal to grades of i and that way we don't write over it now what i can say is grades of i is equal to grades of i plus 1. okay now that puts the 70 into the 60 position and now i need the 60 in the 70 position so i would say grades of i plus 1 is equal to what it's equal to swap that's that variable where i was holding the 60 off to the side and so i would say swp okay and this is the code that will change two positions in the list and so the first time through what is this going to do well it is going to take the 6 the first time through the loop it is going to take the 60 and change it with the 70. so i'm going to have 70 60 okay and then it's going to compare the 60 to the 80 and it's going to swap and so then it's going to be 80 and 60 and then it's going to compare the 60 and the 90 and then it's going to be 70 80 90 60. okay so it's gonna it's gonna basically just kind of bubble that 60 from the top of the list down to the bottom of the list and everything else is going to go up by one now and am i in a better position than i was before yes because at least the low grade is all the way down here but now what you can see is if i just go through the list one time it's going to trade 60 for 70. it's going to it's going to trade a few it's going to kind of get them closer to the right order but it is not you're going to have to go through again and then the next time through it should take the 70 all the way down to the second to the last and then it will do it a third time and it'll be in the right order so what do we need to do we need another for loop and this is going to be again for i in range okay and this is going to be an outside one we still always want to start with 0 all right and then we want to increment by 1 but now we got to think how many times do we want to go through this list well you got to see the first time through it's going to put the 60 in the right position the second time through it's going to put the 70 in the right position the third time through it's going to put the 80 in the right position and if the 60 the 70 and the 80 are in the right position the 90 has to be in the right position so we don't need to go through four times we don't need to go through num grade times we need to go through what num grade minus 1 num grade minus 1 again because again you'll get the 60 right you'll get the 70 right you'll get the 80 right and if the 60 the 70 and the 80 are in the right spot 90 has to be in the right spot so you think about that let's come over here and let's try to code this i hope this makes sense we will come over to our most excellent visual studio code man guys this might be a two a two cup uh lesson here we might need a little extra jet fuel at this point i hope that makes sense to you if it doesn't go back and watch it again and think it up think about it a little bit but we're gonna come over here now to visual studio code i do believe okay maybe i will need to call this up okay there we are now what we're going to come over to is we're going to come over to that average grades program that we had done before because already we're inputting the grades we are printing the grades we are averaging the grades we're printing the average and we're printing the high and we're printing the low if you don't understand this go back and watch the last lesson where we developed this and so the last thing that we did in this code was to find the high grade and print it and find the low grade and print it and so now we need to sort and so we are going to need that outside loop that basically you know corrects things one at a time so we're going to say 4 i in range and then where we're going to go from zero and then remember we only we need to do it one less than num grade so we're gonna say num grades for zero to num grades and then we need to not go through that many times but one less than that many times and then we are going to need to increment by one okay and so that is that outside loop and every time through the outside loop it's going to get one number in the right position now in each one of those we need to go again for i in range and we need to do our swapping okay and so again it just happens that in both cases we need to go through num grades minus one and one all right and our friend mr colon don't forget our friend mr colon all right and now what we need to do is we need to do an if statement if and then what grades of i not grades of 1 grades of i i made that mistake last time and it was a very bad mistake if grades of i is less than grades of i plus 1. so if this grade is less than this grade they're in the wrong order and i need to swap them okay so if grades of i is less than grades of i plus 1 if this grade is less than the next grade i need to swap them because we wanted to order them in descending order so if grades of is less than grades of i plus 1 we want to swap them so what do we do we say swap which is that little holding variable it's going to be equal to grades [Music] of i because we don't want to lose grades of i and now we're going to oh i keep doing that man that is going to kill me this is a horrible a horrible error to do that okay so swap is equal to grades of i and now grades of i is going to be equal to the next one grades of i plus 1. right they were in the wrong order so i'm going to take grades of i plus 1 and i'm going to put it where grades was now if they're in the right order it'll hop over this if statement and just go to the next one so if grades of i are less than grades of i plus 1 swap is equal to grades of i now grades of i is equal to the next one grades of i plus 1. and so right now grades of i and grades of i plus 1 are the same and so now i've got to make grades of grades of i plus i plus 1. it needs to be what that swap variable and so now this line of code will take these two numbers and switch them okay it will take those two numbers and it will switch them okay and now that will step through the list switching all the things that are in the wrong order it will bubble the lowest number to the bottom of the list okay but that now has one number in the right position then it has to come back and it's got to go through the whole list again and then the second to the lowest number will be in the right position and then the third to the lowest number will be in the right position and then by definition the top number has to be the highest number and that's why in this outside loop we just go through num grades plus one okay now that should do it so what we need to do is we need to print those grades out so i'm going to say print and then i'm going to say your sorted great your sorted grade guide your sorted grade list is like that okay hopefully i wasn't covering up any important code you can see what i did with the grades of i plus one there all right and now i need to step through and print it print the sorted list so then i'm going to say for i in range right i start at zero i want to go all the way through num grades because i want to print all the grades and then increment of one who's your friend mr colon and then i'm going to print grades of i and this should be the sorted list all right this is a little bit frightening i'm not gonna lie to you because sorting is hard to do it's hard to write a sorting program in python anyway it's even harder to do it live in front of people but let's give this baby a shot and first of all let's hope that it compiles hope it runs how many grades do you have okay let's say four and i'm going to put them in the wrong order i'm going to put them in 60 and then 70 and then 80. and so this is the worst possible order right 90. hold your breath [Music] boom who's your huckleberry i'm your huckleberry do you see that look at that we started out with the numbers going uh 60 70 80 90 and it says your grades are 60 70 80 90 and then it says your average is 75 your low grade is 60 your high grade is 90 and your sorted grade list is 90 80 70 60. okay man it seems so hard when you're trying to think through it and you were probably doing all types of insanity and all types of nonsense but really it was just like let's see one two three four five six it's like seven lines of code did this right now as i said before there are just some crazy things that you can do to make this run more efficiently because like if you think i'm going all the way down the list every time even when i know that that last number is already right and so there's ways that you can do this sort that takes a lot less computation power and it's a lot more efficient but what i wanted you to see was just understand how to sort grades first and then uh you know we'll figure out later on how to do things more efficiently now remember we always want to check our code so we better run this a few more times and like one of the things i want to do is what if we start in the right order let's make sure that we don't mess it up and so what i am going to do is i'm going to say that i have four grades and then i'm going to put them in the right order 90 80 70 60. and look at that average is 75 low is 60 high is 90 and it goes 90 80 70 60. so if they start out in the right order it doesn't mess them up now let's try to do it in more random order and just make sure that it works for that and so we'll come and kill this and oh that was i don't know what that was but all right so let's come in and let's run it how many grades do i have let's say that i have seven grades and let's say i have 20 and i have 90 and i have 15 and i have 77 and then i have 95 and then i have let's see i have a hundred okay and then i have 28 like that all right now let's look at this your low grade is 15 that looks right your high grade is a hundred your sorted grade list is 195 90 77 28 20 and 15. uh-huh that works okay guys knowing how to sort a list is really you really really really got to think and i hope that it made sense as i was going through this explanation if it doesn't you know guys if i just jumped in here and started coding it would make no sense at all but go back and watch the first part of the video again and make sure that you understand this make sure that you understand what is going on here with this explanation and then what your homework is for next week is is that it kind of made sense when you watch me do it but you need to know how to do this on your own so what your homework is is go back and watch the first part of the video again where i kind of explain the algorithm okay and then turn the video off erase your program and now write the program without looking at the one that i wrote and without watching me do it because you need to think through it enough that you can do it on your own without watching me and that's enough of the homework because again this is a little bit of a tricky thing that we're trying to do okay guys i hope you're having as much fun taking these classes as i am making them if you like the video give me a thumbs up that always helps me with the old youtube algorithms helps my videos get featured a little bit more and then also if you've not subscribed already make sure you hit that subscribe button when you do make sure you ring the bell so you will get notifications of my future video releases and then share this with friends think about maybe posting this video on your social media all right guys this is paul mcquarter from toptechboy.com i will talk to you guys next week [Music]
Info
Channel: Paul McWhorter
Views: 8,853
Rating: undefined out of 5
Keywords:
Id: NX4YYZAaIS8
Channel Id: undefined
Length: 26min 59sec (1619 seconds)
Published: Wed May 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.