 Let's continue with chapter 12 where we're going to do the sequential search In this chapter, we're going to be doing searching in the next chapter, which I will probably cover tomorrow I'm going to be starting off on talking about sorting things So what we want to do is we want to search through an array and find out Where a certain object is? The book uses a deck of cards. I'm going to use something a bit different here I'm going to use a list of countries and you'll notice that they are in no particular order and here I have a Program where I say give a country name. I do this repeatedly and Until you press the enter key I'm going to find out where it is in the list or if it's not in the list. I'm going to tell you that as well Okay, so what we want to do is we want to say we're gonna have an integer called found at Which is where something was found and that's going to be the result of calling the Sequential search method and giving it the array that we want which is going to be our country's array and Then the target that we want and the target that we want. I'm sorry I'm getting things mixed up here We're going to give it the array that we want and then we're going to give it the user's desired choice If the found at position is equal to negative one, then we're going to say Let's do print f Is not in the list Which will be the user choice Otherwise We'll say It's found at index percent d and that's going to be the user choice and the position where it was found That means we now have to write a method to do the sequential search It's going to return an integer and the name again is going to be called sequential search We'll have a string array and we'll call it items And then we will also have a string which is the target that we're looking for Let's document this so that we know what it does And so given an array of items and a target to look for Return the index At which the target was found Or negative one if not found And I think I'm going to copy this from the book here so that we can say a bit of time here And the way they do it is an interesting one. I may rewrite it in a different way as well Let me go here oops Paste that in beautify and copy it in here Oh, they called it search But i'm going to call it sequential searching So we're going to do here is items dot length and if items dot i Equals target will return that index otherwise return negative one And let's compile that And let's run it So for example, we say to volu. It's an index zero Uh, let's give a country name like, um, albania Albania was an index number 19 1920 well, do we want to count them? Sure zero one two three four five six seven eight nine ten 11 12 13 14 15 16 17 18 19. Yes, albania is number 19 Unfortunately, you couldn't see me pointing my finger at the screen, but trust me. I counted it correctly And let's see if it'll find the last one correctly also Run again here so as I kenya Is the last one good now let's try one that's not in the list like germany And it's not the list because it gave us back a negative one So there's our sequential search Essentially it starts at the first item and keeps going through them until it finds the one That we have if it's not found it returns a negative one Now I'm going to say something here and this is Some of this is going to come up a lot if you take computer science 76 But I'm going to give you sort of a preview of what it is. There's something is how do we figure out How efficient an algorithm is And we use something called a big o notation The o stands for on the order of for sequential search On the average you need to go through half of the entries to find the target Sometimes you'll find it right away. Sometimes you'll have to go all the way to the end But over the long run you have to go through about half of the items and The number of comparisons you have to do Is proportional to the number of items in the list So a sequential search Through a list of a thousand items will take About 10 times as long Again over the average as a search Through a list of a hundred items And we say That the sequential search Is an order in algorithm So when I say big o and an n in parenthesis that means Proportional To the number of items And again, we're going to see big o notation coming over and over and over again In computer science 76. So Now you've heard about it. Okay. There's our sequential search Um, I was gonna say there's another way to write this I may come back to that later if I have some extra time to do that The thing that's bugging me a little bit is I've got two return statements here and there's this school of thought that says A method should have one entry point and only one exit point And the more exit points you have from a method. That's more points of failure for things to go wrong There is a way to do this. So you have only one return statement But it does not look as elegant as this So I'm going to leave this one for now the way the book has it and I might come back to it later Okay, when we have things that are not in order, we really don't have any big improvement over the sequential search We have to start at the beginning and go all the way to the end And either we're going to find the the thing we're looking for or we aren't However, if the number of countries were in order As they are here Where I have albania through zimbabwe Then I can take advantage of something called the binary search And here's how that works Let's just go with a few countries here. Let's say I have albania Well, I'm just going to use the letters. It'll be a lot easier. So let's say acf h k m o and q r and z Uh, let's go to z and z there we go Now these are in order the way I can look for it is I can go and start See me take me a couple of seconds to move myself to wherever So let's say I'm looking for the letter r I'm going to go to the middle element Yeah, r is bigger than m. I'm counting on the fact that everything is in order The fact that I have m here r is bigger than m that means that all of the ones before m are no longer in the competition I can ignore all of them. So I've immediately gotten rid of half of the half of the Things I need to look for Now what I'm going to do is I'm going to smooth this up a little bit to o Because the m is not the one I'm looking for either And now I'm going to go and look at what's in the middle of these And it turns out that r happens to be in the middle of that group there And I found it but let's say I was looking for t so just to keep going on this Since t is bigger than r that means I can get rid of this half so So when the items are in order I can go to the midpoint Okay, if the thing I'm looking for is before the midpoint Then everything from the midpoint onwards Is no longer in the competition if it's after the midpoint Then everything before before the midpoint Is out of the running each time I do a comparison Then I eliminate half the possible choices Either I eventually find the item I'm looking for or If we have this let's call this the start and the end Or the start and end points Meet which means I haven't found the item This algorithm Is proportional to the log to the base two Of the number of items So for let's say 1024 items, which is two to the tenth It will take at most 10 comparisons To find the um item or find that it's not there If I have let's say four times as many 4096 items, which is two to the 14th It will take at most 14 comparisons If I have let's say 32768 items That's going to be two to the um Oh, what is that going to be here? I'd be able to think here To the 15th Oh wait, no, no, no. I'm gonna I screwed up on this. Excuse me This is to the 12th pardon me So this is really quite nice If I have the things already in order then the more items I have I still don't Have a huge number of comparisons to do whereas here 32 768 if I were doing a sequential search on the average I'd have to go through 16 000 of them Whereas for 4096 I'd have to go only through 2000 of them on the average And so it turns out that the binary search is order of log to n but again It requires that the elements Are sorted into order already And why is it called a binary search? I guess it's called binary Because you lose After the items Every time you Do a comparison Okay, so let's go and do this. Um, let's come back here and in this case Let's use our same code here But this time we're going to call it binary search and I guess I'd better get my indentation correct here There you go Now what I need to do is I need to write my binary search method It's going to also return an integer And we're going to give it. What was the name? What were the names I used here? An items and a target perfect And again, I'm going to go to the book and use what they have there And change a few things here And let's go back here Oh, whoops better better prettify it Prittify it That's going to be items dot length So as long as the low and high Pointers the low is going to start at zero The high is going to be at the end of our array And as long as they don't cross over each other, so I better change my notes here Um, I'll start it in points Exchange places Which means I haven't found the item I'll find the midpoint Then I'm going to compare Uh, excuse me. There's something something's very weird here. Oh, dearie me It appears that copy and paste copy and paste is not my friend Because it looks like things have gotten out into the wrong order Yuck. Okay. I'm gonna have to clean this up here. And let's see what the head here Return minus one out there. Okay. Okay. This is much better So while the low is less than or equal to the high point I go to the middle I compare the item at the middle to my target If it's equal to zero then I must have found it I can return the midpoint Otherwise If the comparison comes out less than zero that means that this item is less than the target And that means I can get rid of everything below The low becomes mid plus one Otherwise the high point moves down To the middle and I get rid of the upper half of my Array for my search And let's compile that Let's see if I copied everything properly here. So if I say here, um, Albania Was it index zero Zimbabwe should be at the end Check to see if it finds that correctly And let's say we have, um Denmark Was it index three? Let's check to make sure that's correct zero one two three good And let's try again looking for Germany And Germany is not in the list Now the book says, okay, let's do some, um Debugging outputs so we can see what's happening here And in fact, I'm going to put in some extra outputs so we can see what's really going on Let's print out the midpoint And then we're going to say system dot out dot print f Compare to percent s Gives percent d We're comparing items submit To target which gave us comp Here for zero In fact, let's do this So we can see the low high and midpoint And then here what we're going to do is So now let's look for example for, um Kenya So the midpoint of zero on 23 is 11 I'm comparing Latvia, which is at the middle to Kenya Latvia is bigger than Kenya which means I'm now going to Get rid of everything from Latvia to the end and now I'm going to go from zero to 10 The midpoint is five France is less than Kenya And so I go from six through 10 Middle is eight Comparing India to Kenya gives a negative two, which means I get Um rid of some of that compared to Japan to Kenya and finally it turns out that this one was almost right in the middle so I have one two three four five um Five comparisons that I have to do Let's run this again And let's look at this and let's say I have something like, um You got one And here it was founded in deck six again, I had to go through one two three four different um comparisons to make it work If I have something like Germany, let's see what the country gets here And you'll notice that everything keeps going until the low and high cross over each other And Germany is not in the list um what might be easier to do here Is and you can do this on your own. I'm not going to do it here during our Session today. Let me see if I can find this um online java Debugger, I don't remember where it was here. It's definitely not this one. Thank you the online java compiler There we go. That was this one here I'll scroll down to compiler on your code. Okay So this thing might I don't know if this is gonna work Let's let's let's give it a shot and see what happens. Okay that in there And this time I'm gonna get rid of the scanner because I'm not sure how to make the input work And for now, we're just going to say string user choice Let's do something like on india and then we're gonna have found out as negative one We're gonna get rid of our while loop all together That makes things a little bit easier And let's see if this I have no idea what happened there. Oh, we have an error scroll down to see the error Oh, during me. I must have gotten rid of something that I shouldn't have. There we go. Let's try that This matches that and this matches that perfect. Okay. Oh during me Sorry about this Okay, and then we can go through this and take a look and so if we go to this one So here we get our list of countries user choices india Now we come in here. We set the um close that Low is zero high is 23 Our midpoint is 11 And we do the comparison that out The person's less than zero. So watch what happened with low and high The high becomes the midpoint minus one and then we come back to here Low is still less than high and now we're looking at zero through 10 And so you can trace it through that way. So you might want to use a visualizer again I'm not going to go through this all the way That may be another way for you to see it happening in action And that's all I want to do for chapter 12 Tomorrow I'm going to do chapter 13 where I'm going to talk a little bit about objects of arrays Which is where here we had arrays of objects I had an array of strings what we're going to be doing in the next chapter is we're going to have an Object that contains arrays inside of it and we're also going to learn how to sort an array And that's it for today