 Okay, welcome everyone to list loops and efficiency with Tokyo EdTech. That is me Today's topic will come as no surprise. It is List loops and efficiency. This was suggested by 16-bit member Kevin He had a question about loops and I think this will answer it but definitely wanted to shout out to him Paul, Yan, Mod, Remod, and Kim Chi-yong My first channel members. I do appreciate the channel support. If you like what you see, click subscribe If you're able to please consider joining as a member and supporting more directly. So what we're looking at today is List loops and efficient search through a list and this is a very important topic in computer science and I'm not an expert on the subject But I think you'll learn a little bit here today So our main question that we have to ask is as the size of a list Increases, how can we search the list quickly and efficiently? So it is I'm gonna go ahead and I'm going to create a list so I'm gonna call this numbers and I have a number down below so seven Six eight Three comma oh comma nine Eight six seven I think I missed the five So basically probably the first thing that we would try is what's called a linear search So you can see over here So let's say we're looking for the number five. We want to know does the number five exist in this particular list of items So you know as a human being we just look at it. We can see there's a five there But a computer cannot do that Computer has to look At least for now at every item and see if it is in fact a five So I'm gonna go ahead and make a loop. So for number in numbers So if number equals five because we're looking for five print I found a five And that point we can just break we can end the loop because we don't need to Do anything else. Okay, so I'm gonna go ahead and run my program And you'll see here It says I found a five because there is a five right here in the list now If you give it a little bit of thought How many items did it have to search? So I'm gonna say Okay, well searched I'll sell searched items We'll say number searched Searched okay equals zero And we're gonna say number Searched plus equals one. There is an underscore here. It's just not showing up On the screen. I got something to do with the font. There we go made a little bit bigger Okay, so we're gonna say number searched equals zero and each time through it's gonna add one and Then we'll print I had to search a number searched Times I'm gonna make that an f-string so that should work Let's go ahead and run that again and for those of you are curious. I am using the genie editor. It's free It's open source. Yeah, if I run that So I found a five. I had to search seven times. So one two three four five six and seven So you can see here It took seven searches Seven times three to find that five So if you think about this logically this five could have been here So if we exchange the five and the seven and then we run it again Found a five. I had to search one times should be one time. I'm gonna go ahead and undo that But what if we put the five out here and reversed it with the nine go ahead and run that Had to search ten times so Kind of you know logically speaking you can see that the more Items we have the more times we might have to search So this is one two three four five six seven eight nine ten items So the maximum was ten the minimum was one So that kind of tells you that the average number would be somewhere right in the middle of that of that number if we're searching so think about this if instead of One number or sorry instead of ten numbers. We had a thousand numbers So I'm gonna do is I'm gonna do this. I'm gonna go ahead and I'm gonna import random What should I what I will do that later? I'm gonna end a bit Okay, so let's say we had to Search for a match. We're looking to see if there was a match Between one of the numbers or between the numbers, okay? So basically does this number equal this number or does this number equal this number does this number equal this number Does this number equal that or etc etc? Okay, so What I need to do is I need to compare this number Pull that up searching for a match So basically what we're looking at is you know, does seven match one does seven match seven doesn't have a match eight six Etc. Etc. We don't want it to match itself Then does one match seven does one not match itself match seven etc etc So you can see how the number is gonna go up and that's what I want to look at here So I'm gonna go ahead. I still have numbers So what I'm gonna do here is I'm gonna do two indexes So for I in range The length of numbers And for J in range Length of numbers, okay, so we want to know We need to look at each of these to see if it compares to all the others Okay, so remember lists started at zero So we want to look at zero and one zero and two zero and three zero and four zero five zero six Etc. Etc. We want to look at one zero, but not one one One two one three one four, etc. We want to get two zero two one not two zero. So we're basically looking at Each of them against the entire list each time so I'm gonna go ahead and do number searched So we get an idea of how many times it's gonna go and We'll put number searched Plus equals one now what we're gonna do if I does not equal J Okay, I don't want to compare Zero and zero one and one two and two three and three so as long as they don't match Then I look and see if there is a match. So if numbers numbers I equals Numbers J Print I J Numbers so what that'll do it'll print the I the index of The first number index of the second number and the actual number itself and since they equal numbers I number J is the same And then it will print. We'll just copy that How many times it had to search? Okay, so let's go ahead and run that type error There should be numbers my bad So not number. Let's go ahead and run it. Oh, wow Okay, so you can see the second one Let's go ahead and print here. Sorry real quick print. Oops linear search Has a nice little output copy that for a match. All right. Sorry, and we will space there Okay, so you can see here Zero and two match there seven zero and five match seven two and zero match at seven two and five match as a seven Five and zero match that's seven and five and two match. It's a seven Which I find very very interesting So you can see here zero two is the same as two zero Zero five is the same as five zero two five is the same as five two So you can see here. We're already we're kind of duplicating a little bit of our effort, which is part of it But you also see here Notice how many searches there were 100 so Let's say I double the number here So I'm just gonna go ahead and copy this and this is super super important for comp site So I've doubled the number I've gone from 10 to 20 so I'm gonna go ahead and run this again and you can see that as numbers there now this one because we are only looking for a Five and it was here But if it had been at the end if there'd only been one five or if there'd been no fives We would have to search the entire list Okay, and so We went from 10 to 20 So the increase was linear. We call this n. This is actually called. Oh, I'm not gonna explain this any further Beyond this This beginner o n. It's a small n. Maybe it doesn't matter. Anyway, this is if you ever see something called o of n This would be o of n as the number increases So if it doubles the search space doubles so from 10 to 20 the maximum is going to be 20 however in this particular search This is n squared because when we went from We had 10 that gave us 100 but when we had 20 that gave us 400 so this would be this would be o and Squared again, I didn't really want to talk about big O, but I'm pretty sure this is correct So you can see even for a fast computer if this was a hundred It would be a hundred times a hundred, which is what ten thousand eleven thousand a hundred thousand About ten thousand so you can see it goes up very very rapidly now, of course, you know modern computer can be searching through List of millions or even you know hundreds of millions of items So let's say you're looking through a list of credit card. Let's see social security numbers There's 300 million people in the United States. Let's say you have to look is somebody using a duplicate number If you have to search through 300 million compare that against every other Number that's gonna be 300 million squared. Okay, so that's even for a fast computer. That's a lot of searching now It can do it But it's a lot, okay So one thing we could try here Because as we saw earlier, there was a bit of a overlap, you know, we had zero two and two zero So we don't need to Search quite like that There is a way we can we can kind of make this a little bit faster. We can cut it a little bit down So if you think about this We can start here and We search here and then against all of these Okay, so zero and then from zero to the end are from zero and then one to the end Then we look here. I don't have to check here because I've already done it So I go here plus one all the way to the end and I go here This would be I and J is plus one I know it's a little bit of a fast thing But basically you can see how we don't have to search stuff that we've already searched So let's go ahead and implement that So this is gonna be I plus one So I is gonna start at zero And I'm gonna look against one Then it's gonna be one And I'm gonna look against all of these then to look against all of these and that's what that does So I don't need to worry about I equaling J because it's never going to happen. I Can get rid of that? And I'm kind of curious to see what this does to the numbers. I actually don't know So when we did a search for 20 It had to search 400 times so let's see what happens here Okay, so in this case it only searched 190 times so we cut that number in half Which is kind of interesting which is which is a start 309. I want to see what happens if we make it 10 So 45 times I can't be right Did I do something wrong with that algorithm, that's right, but it searched 45 times number search number search Oh, I did not expect it to be that high, but it did cut it in half notice that So instead of 100 times it only searched 45 times If I made it if we'd made it 20 so it went down by half then this went to 190. Let's put that in there So was that 45? I'm sure some mathematical formula. I don't have it in my head right now. I apologize I think it was 190 it said so you can see how I cut it in half But it's still a lot Okay, it's still basically going up exponentially. It's just going up slower exponentially Okay, so if we made this higher it is still going up at a very high rate so this is I Don't know if we make this one anymore efficient except well except by doing what I'm about to do okay, so Those are kind of like linear type searches and Searching against other things in it. So you can see there's a two different types of growth here We've got an on which is linear. So as the number of items increases The size of the search increases at the same rate. So it's a constant rate. So if it's 20 it goes to 40 then It goes 20 to 40 it doesn't go 20 to you know 400 for example You can see here. We've got n squared. I think that's right It's gonna go up So as the number of ends increases, so we went from 10 to gave us a hundred twenty gave us 400 instead of 200 so it wasn't a linear it was linear. This would be 200 not 400 Which is yeah pretty scary. So how do we search if we have huge large numbers of numbers? okay, so for example let's say your credit card company and Somebody tries to buy something online They use a credit card a credit card companies can have millions upon millions upon millions of credit cards So there's a hundred numbers or a hundred million numbers How can we search a hundred million numbers in that short time that we have because people will not wait as you know one way is to use a binary search and a Binary search lets you search a large number of sorted items Quickly and that is the key term here Sorted, you know, I haven't programmed one of these in ages, but I'm gonna do my best so we have a list of numbers and First we need to sort it Okay, so I'm gonna go ahead and just go do it. So I say numbers dot sorts I'm gonna ignore the fact that it takes time to sort. That's a whole other question I'm gonna go ahead and print numbers and see what we have here So I'm gonna print numbers and then sort of print Numbers when we go ahead and do this this binary search Okay, if five, okay, so we start out with this and now we have this Okay, so in this case Let's go back to our original case if we're looking for a five in that case We had to search seven times Again, that's assuming we found it. If we hadn't found it, we would have had to go all the way to the end Now a binary search works this way I have a list and it's you can see that it is sorted I'm gonna look at the middle number In this case it is six Okay, so even that's we had to pick one six or seven doesn't matter But let's do six So I'm looking for a five So the first thing I ask, does this number equal five? If not, okay, I haven't found it Now, is this number greater than five or less than five? Six is greater than five. So what that tells me is I can get rid of this entire list You never have to look at this part of the list again So then what I would do is then I would go to the halfway point here Which in this case is gonna be three I'm gonna do the same thing. Is this five? And if it's not, I do the same thing Is this greater than five or less than five? And then I'm gonna, since it's less than five I'm gonna look at the halfway point between my last one and this one And this is five So that took me three checks Took me three times to get to that I hope that makes sense So I'm gonna try and program this again I haven't done this in ages So I don't know, I can't remember quite how to do it So what I'm gonna try and do here is I'm gonna get the length of numbers So I'm gonna say index, let's say i i equals length numbers divided by two And I'm gonna use round here To make sure I get the proper number So let's go ahead and print i just see what happens Okay, five, so that's zero, one, two, three, four, five So that's gonna give me the seven Okay, now, let's see here I'm gonna try this Okay, I'm gonna try this, we'll see what happens I'm gonna do a while true loop There's probably a better way to do this But I'll try this one for today So, ah, I need to put the number I'm searching for So target in this case equals five So if numbers i equals five Okay, print, I found a five Break, I'm done, I don't need to search anymore Okay, now, LF numbers i is greater than the target It's greater than i equals i divided by two So that means I wanna go So I'm here, this is greater than the target So I'm gonna cut that in half And go back to whatever that's gonna be And then what I wanna do is And then it'll just come back around again And look for the five The third case is LF numbers i is less than the target And i is gonna equal round i plus length of numbers Divide it by two Confident this is gonna send my work Now normally we would do this with recursion But I don't think most of my viewers Or particularly my students would know how to I know my students don't know how to do that yet And I think this will do it Continue And why I'm doing continue is because I wanna go back to the beginning of this Yeah, I know there's one problem with this It has to deal with the case where we never find it But I'm gonna just assume that it exists Because otherwise it'll just run forever Okay, so I'm gonna try this and see what happens Now it found a five I'm happy about that But we don't know how many times it took So I'm gonna go ahead and copy Number searched So number searched equals zero Number searched Plus equals one And then I'm gonna say And we just wanna print out how many times I had to search So let's go ahead and run it one more time So you can see how it's searched four times So let's see if that's correct So we started Well, let's print out I each time So print So we can see what's going on there Okay, so it started at five We printed five twice Then it went to two Then it went to six, then it went to three So it went from zero, one, two, three, four, five Started here Then it jumped down to two Halfway point Okay, there's an error in my algorithm I see what I gotta do I didn't discard the rest of this Now I could have done it with numbers But I didn't So that was bad Okay, so I'll do it a little bit differently So I'm just gonna cut that in half And say numbers Equals Numbers So if I is greater than the target Zero To I Dot inside I think this is about right No, it's not right Yeah, this is getting messy Again, I'm sorry, I know I didn't Prepare this one ahead of time Yeah, maybe a little overconfident Anyway Hoping this will work Let's try this I come a length of numbers I just do I Yeah, just do that And I can just do this So that should hopefully cut everything in two Let's try it Now we're talking Okay, I think that worked I don't need to print I anymore Okay, so let me run that And see if I got what I wanted Okay, so what happened We went to five so zero one two three four five We started at seven Okay, seven was not right So then we went We cut this part off And then we went half That gave us two Two was too low We cut this part off And then we get another Two That gave us this And then cut that in half That gave us one I think this is working right So what I'm going to try Is I'm going to actually make a random list of numbers Okay, so I'm going to cut that out Well, I'll leave it I'll leave that there I'll make a new list I'm not going to print the numbers I'm going to be making a very large list of numbers So what I'm going to do is I'm going to say four Say numbers equals blank And I'm going to say Actually, no, I do need to do that Sorry Because I want to compare the three So I'm going to hide this I'm going to say numbers Four blank in range One zero two four Numbers plus Numbers dot append I'm going to choose a random number So x equals random dot Random int Zero to Say four oh nine six So what this is going to do This is going to give us a random number Between zero and four thousand ninety six inclusive And then what I'm going to do is I'm going to Since we're looking for five I'm going to Numbers dot append five So we know we know there will be At least one five in this In this thing Okay So let's go ahead and see if that works Random is not defined I got to import random Import Okay, this is what I wanted Excellent Oh my gosh Okay So apparently there were a lot of Matching numbers in there Okay So So you can see here With the linear search It had to search five hundred and twenty four times To find a match So clearly there was a different five in there Not the one that I added at the end Because there's a thousand twenty four numbers on there And then searching for the matches It had to search Five hundred and twenty four thousand eight hundred times Because there were twenty one thousand twenty five items On the list I think But the binary search to find the five Only had to search nine times And that's the power of a binary search So even though we had one thousand twenty four We cut it in half To five twelve We cut that in half to two fifty six These numbers just happened to match We cut it in half to one twenty eight Or does it always match Sixty four Boom And we finally found a five So if I go ahead I'm going to go ahead and make this I'm going to double this So I'm going to clear this I'm going to go ahead and double the number To two thousand and forty eight And we'll see what happens So you can see There's lots of matches In this particular case Now we can't see the printout Since we're not really looking at The match thing But you can see how those numbers go up I'm going to go ahead and not print that I'm just going to go ahead and put pass It'll still run But just won't print it out Okay so let's run that So for our linear search In this case three hundred and thirty times So that tells us there was a five At a three hundred and thirty spot The binary Sorry The searching for a match Two million ninety eight thousand times Now it's fast The computer's fast The binary search had to do it Only ten times So we're comparing the binary search To the linear search So you can see how much faster it is So let's go ahead and make this Let's make it a really really large number I don't know if the computer can handle that Let's make this a large number Three two seven six eight as well So hit five See how searching for a match is waiting It's still running That's how many items it's searching for So there's only one five Because they had to search thirty two thousand seven hundred and sixty nine times Now the match part You can see how long it's taking It's still running I might have to stop the computer I don't even know how long it's going to take This is one of the things where I should probably edit the video But I can tell you I will not But because it's exponential It's thirty two thousand seven hundred sixty eight Basically squared divided by two Is what we've been seeing Roughly It's still a very very large number But the linear search It found a five So there was only one five Because it went all the way to the End of that list I got a little ambitious with the thirty two thousand I guess But I do find this kind of stuff very very interesting But what we're going to really What we want to really see is the binary search So instead of searching thirty two thousand seven hundred and sixty nine times How many times will the binary search have to search Okay maybe I'll stop that So Let's change that number to Well anyway why don't we just get Just comment that part out Actually I don't know how to do that in this one To edit Insert comments Comment lines there we go There's probably a keyboard shortcut I just don't know it Okay so we're looking at our linear search And we're going to compare that to our binary search So we have thirty two thousand seven hundred sixty eight Okay so Again it had to search thirty two thousand seven hundred sixty nine times And Well this algorithm only had to search thirteen times Again this is because it is sorted Okay so the sorting is very important to a binary search Let me go ahead Let me just go ahead and make some big numbers and see what happens here So you can see here let's make this Let's make it Three six And see how many times it takes So twenty seven thousand so it found a five in there somewhere But the binary search only took Twelve times So twelve versus twenty seven thousand five hundred and eighty four Because this is linear And this is called log this is log base two I won't bore you with the math but long story short it's way faster Okay so even if I made this number just ridiculous Let's try it So that's how many millions So that's one hundred thousand That's six billion numbers So let's see I don't see what happens I'm kind of curious Okay notice we're waiting For the linear search To find a five Actually no we're actually waiting for it to make that There's probably enough it's probably the memory issue Let's see what happens Or it could be actually the sorting I forgot about that part Let's go ahead and kill that Yeah I forgot about that part So yeah because we actually sorted it takes longer I kind of glossed over that But okay so there's one million Let's see what happens Okay so that was a little bit of a drain So Eighty four thousand There was a there was a five Eighty four thousand And then once we sorted it And then did the binary search It only took fifteen times So the key thing here If you're gonna do binary searches Is when you add an item You should add it sorted Because you don't want to sort it At the same time as well If you can sort it you can search So it's much better to start with a sorted list And that's what was taking all the time I think So anyway I hope that helped Again my purpose wasn't quite so much to talk about the code on this one But you kind of get the idea And again this is something that normally you would do with recursion And there's actually another way to do this I could use I think two variables I just didn't Silly me But this was I think was easier to understand So just kind of a quick review If you have a list of a lot of items And you want to search for it You know if it's a small list You can do a very quick linear search Okay if there's you know hundred items It's not really going to matter Unless you're doing it you know a thousand times a second So you have to really think about The problem that you're trying to solve But there's different types of growth And o of n So big o n means that As the number increases The time increases linearly So you go from ten to twenty The time goes from you know let's say it takes five seconds You go from ten to twenty Time will take ten seconds because you're doubling it So it's linear There is Exponential growth Where you have you know some exponent here Usually it's like n squared So where you're searching two things against each other And you have to search everything against everything else Even though we can cut it down just a little bit By about half It's still growing At this very very fast rate So you're seeing exponential growth And then the last one was a binary search Which is big o log I think it's log two I could be wrong on that If anybody knows you can comment down below Something along those lines Log n sorry So I think it's I think this is it O log base two of n Pretty sure that's what it is So because it's It's the way logarithms work Well I'm not going to do you worry with the math But anyway that's basically it Pretty sure So because you're cutting it in half each time If you you know you double it Then you're only adding And each time you double the number You're only adding one extra search Because you're cutting it in half each time So it's it's pretty darn powerful Assuming that the list is sorted The list isn't sorted A binary search will not work Anyway Kevin I hope this Gave you a little bit of an idea I don't know if you wanted this is a little theoretical Or if you wanted it a bit more practical On game type programming But this is an important topic We're going to if you're really looking into computer science So I hope this helps some people More than it hurts Again I know this is a little bit messy code But I think it's followable I think so anyway Have a good night Subscribe if you haven't Join as a member if you can Keep on coding take care