 So now we've been talking about working with arrays and one of the things that we do what now that we have them is this idea of Searching algorithms and one of the big ones one of the main ones one of the easiest ones to think about is Something known as a linear search algorithm now a linear search algorithm All that happens with a linear search algorithm is I have some given key I Have some given key. That's presented before I go through my loop and then I literally am going to go through element by element until I find that element and When I do I'm going to return the index of that element So the index of that element now right now. I've started out my Search my linear search. So my index is at zero. Well, my key is at three right now So I say does list at Zero at the index equal my key Well in this case. No, it does not six does not equal three. So what do I do? I increment my index and I move on to the next element and so this time suddenly index has changed to One and suddenly I'm now looking at my four. Well does my four equal three? No, it doesn't so I keep on moving Again index has been incremented to two One is at the two spot. One does not equal three. I keep moving. I keep moving I keep moving until I get to index five and index five guess what index five Equals a three three equals three. So what I get back is Five So how do I apply this? How do I put this into actual practice and the same idea comes into play? We're still looking at this and I'll let me move my Disembodied head for a second. Let's see order position move to the top. Ah ha So again the same kind of concept comes into play. The first thing I'm going to do is I need to create an Array of random numbers now Why do I do random numbers of all because I just needed to throw out a hundred of them and I want them to be all Integers so again random numbers. I'm just making random numbers from zero to 49 and then I'm saying I want to look for when my key is 25 wins the first occurrence of 25 in this randomly generated array Now one of the things you might notice is I'm also including this right here Int key index equals minus one my okay, huh Well, if we think about Indices for a second We're never gonna have a negative one index. We always start at zero The idea you can think about is you can never have negative distance Let me think about that for a second. I have a Dot right here, and then I have another dot right here Alright, well the distance between those two will say is two Well, if that's two then what's this? Well, guess what distance dictates that that's still a positive number. It's still two units away from this guy So the same thing actually applies now when we're thinking about Linear search. I'm basically saying that I know that I'm never never never ever going to magically have a negative one index for my linear search and As the developer what I can do is I can go ahead and plan ahead And say well if I see that negative one For example, I see that negative one or I don't see that negative one That's something that I need to go ahead and account for Then what I do is I basically like I said just like we've done up here I traverse my array I go through each Element in my array, and I say does that element at I at I Equal my key does the element at Zero equal 25. No does that element at one equal 25. No two. No three I keep going until eventually list at I equals 25 if it does what I do is I change Key index to whatever that I is now notice the next thing I have is a break and the reason why is because now that I've found the first occurrence of the number I want to go ahead and exit out of my loop say for example. We look at this code again What happens if instead of this being a two? It had been a three Well, if I don't do that break what happens is I immediately move to the next element and guess what? I just said that this was a three so three equals three now suddenly my index is six instead of the five I want the five not the six in this case, so I want the first occurrence So I use this break right here to do exactly that then like I said, I've got this check at the end I'm basically stating is My key index did I find 25 inside of my array and in this case if it does not equal negative one It was found hooray, and I'm just seeing for a second that it's not showing it. So give me just a hair I'm going to fit this into one screen recent size that didn't help give me another and three two One ah, there you go. You can see that again If it is negative one hooray if it is not or if it is not In the array it is negative one the element is not in the array