what's up guys this is Teddy welcome to my YouTube channel this video we're going to be talking about linear searching an array in csharp and this is probably the easiest search algorithm that you're ever going to use it's incredibly real world if uh this is not something that you're just going to be doing just for the fun of learning like you will use a linear search in a production environment I can almost like bet like I'd bet like aund literally I would bet $100,000 you use a linear search in your programming career at one point
and I'm not that there's no guarantee but you get the picture linear search I call it the human search this a linear search would be if exactly how you would search something as a human so in computer terms first we would make a for Loop so let me see here so we just go you know first we would just make a full loop and we would just iterate through it and just like a human we're just going to go through each one so human is just going to go one is it okay and you're going
to search for let's say we're going to search for six so we're going to go one no not here that's not there two no not there three not there four not there oh here's the S oh here we go six we return true and that's going to be the actual looking through each one is going to be the for Loop and the actual checking is going to be an if statement so so the actual picking it up looking at it being like is this a six that's the if the four Loop is the actual you
going to each one so one two three and then the if is going to be I'm checking you know what is actually in there and makes does it make sense now it's going to make sense when we actually get in get into it and we actually start building this so obviously being that we actually need an array to look at we're going to go in here we're going to actually create this array just create like a dummy array and you you can literally popul you could populate it with strings if you want to you could
populate it with integers objects that's a topic for another day but if you want if you're feeling a little crazy and you're feeling like an algorithm Master we could do some objects one day not in this video because this is kind of aim at beginners and we don't want to freak people out then we go eight we go nine and we're just going to have this array of this many values right here and we're just going to initialize it you could create a for Loop to do all this for you but I think sometimes it's
easier in cases like this to actually um just initialize them with the values already inside there so we're going to go here we're going to call this a function then we're going to pass in an INT array and then we want to pass in a key and a key is a fancy word for I'm actually going to comment this key key means what value you are searching we're we're searching an a searching algorithm so we're we need an actual value to pass into this algorithm to actually um look to we need to we need to
actually search for something if there's no key we're not actually searching for something let's just you know describe it that way so key means what value we are searching for okay and we are returning a bull we're returning a bull you could return a negative one you could return an INT you could return a console you could do any of these but I think in this case where're a search is we are and I forgot that R A search is true or false and I think that that's it you know what you're going to see
out there in the wild so to speak so all we're going to do here is we're going to create an i and we're going to go zero we're going to go I array. length so this array is going to get passed down here um a like the most basic for Loop we could possibly have we're like I said we're just like a human we're just going to go through each one one two and we don't need any special type of array or anything and then as I mentioned in the visual this and I'll show you
and I'll break this down here in a second and really show you what's going on it's going to return true and then this is going to be an array this is actually going to be the array that we passed down we're going to pass in true so go here and if this whole entire the reason that we put a false on the outside is because if this whole entire Loop runs and we don't have a value that means there's nothing in there and we're going to return false and let's go here and we're just going
to do a console right line so we'll go console. right line we're going to go linear search and we're going to pass in our array and a tell sense is awesome and you may be wondering well why did you put this why did you put this in a console right line and I actually made this I made this goof like a long time ago I would just have this on the outside like that and it would never display anything and I'd be like well why is my program not displaying anything because you have to wrap
it in a console log otherwise it's not going to display anything in the console so we're going to go here and let's go ahead and going to see let's execute this thing we're going to go through it step by step and I'm going to show you every single part and also we're going to blow away these these watches and we're going to create our own watches and that's always a good thing that I always tell people create watches in Visual Studio or whatever IDE that you're in and it will save you a lot of headache
because you can actually see what's going on and let's see then we're going to go we're going to put a watch on this length and it doesn't exist yet so we're getting an error then we're going to put a watch on this key so add a watch then for just for shits and giggles we're going to add a watch to this eye and we're just going to just watch what happens so we've already create so it went through we've created our array it's initialized that array and you can see all the values in that array
in memory and I need to go through that one more time because I need to step into it so what you want to do is when this Arrow gets to the console log you want to step into because you actually want to go into this function and I actually just made that mistake so it's going to initialize our I then we're going to have our array. length and if you notice there it didn't actually run that increment because for some reason and I don't really know like why that's true it knows not to increment it
the first time it only knows to increment it on the second one how it does that I don't really know but you can watch it in real time as we kind of go along so we go array then it's going to go within our array and look at the I and the I is zero so where is the I going to go it's going to go inside here and it's going to check for the key the key is zero is this array zero like look at look at right here if you ever just get confused
you can be like oh where am I at so I'm at I and this is zero and then in here this is zero so we're at one is key equal to let me see here equal to zero no so it's going to run again and and it's not going to be true and then it's going to increment it it's going to go through it's going to check it again it's going to go through array is array one and we check here is one equal to key which is going to be zero is that value equal
to our key that's going to go through no it's going to go through again it's going to check it's going to actually do the increment and you notice here it's not this has already been assigned how this is happening in a comp underneath and in the compiler to be honest with you I really don't know but if you know how that all that's working I would love to know because it's a very um that's a question like I think of all the time because I'm stepping through four Loops like all the time so as you
can imagine it's just going to go through each and every single one it does not find it should not find it and we're let's see what value we're at nine right now and then should step through the ray. length is equal to 10 it's going to step through it's going to be false and there's no value in there so we've got false right here but let's change our key to match something that actually works and let's change it to a four and let's see what actually happens when we execute it so we're going to go
in we're going to go down it's going to initialize our array we want to step into this linear function if a function is wrapped within a console right line you need to press step into at the console right line or it's not going to work so we're going to go ahead we're going to step into and let's see where we're at we're at we're at one right now in the array and it's supposed to go through the length of the whole entire array which is 10 so checking checking and boom we've got a true and
you can see here three is three equal to what's the key so we go into our third one that is number four the key we passed into the linear search is number four and we're going to return true and it returns true very simple algorithm very real world I highly recommend that you learn it if you guys enjoyed this video make sure to hit that like button make sure to hit that subscribe button and as always thank you for watching