Hey, guys, what's up?
So in this video, we will learn how to search for a given key in an area.
OK, so what will happen?
We will be given an.
So we have an ETA letter that elements are 10, 20, three, four one zero.
OK, so we have an ETA and let's say we have to search for 20.
OK, so we have to search for a given value in adding.
And we have to tell whether the given value is present or not present, so zero, one, two, three,
four and five.
So these are my indexers.
OK, so, for example, in this case, 20 percent, some output will be one, OK, one means index that
is going days present at index one.
So, for example, if the key that we have to search for is a 30, that is not present.
So Melbourne should be not present.
Or you can say Guinot found Key not found.
And here we have found key.
At position.
One, OK, so user will give us an array and user will give us for the value that we have to search
in the area and if the given key is present in the array, you have to print that.
Yes, the given key is better than that, and it is present at position one.
And if the given key is not present in the area, then your output should be the given keys, not present.
OK, so this is the task that we have to perform.
So how we can solve this problem.
So the algorithm for solving this problem is very simple.
What we will do, we will scan that.
We will scan array from left to right.
OK, so starting from zero to end minus one, we will scan the Eddie, for example, my arrays, let's
say five four two one three zero.
And the value for which we have to search and let's say my key is to.
So what we will do I will check is five for close to know is very close to know.
Is too close to.
Yes.
To be close to stop.
So ultimately, what we are doing, we are getting that from left to right, from zero to index and
minus one.
Similarly, if we have to search for, let's say zero, so it's five equals zero.
No, four equals zero.
No, two equals zero.
No one equals zero.
No, three equals zero.
No, zero equals zero.
Yes.
Key found, key found at position, we found the key position, so zero, one, two, three, four and
five.
So key found at position five.
OK, suppose the given key is, let's say.
Six.
OK, so Jack is five equals six now is for equal six now is two equals six, nor is one equals six.
No ethical six.
No is zero.
Equal six, no.
So I will reach here.
OK, so I will reach our position.
And that means gay sex is not present.
Sex is not present in the Eddie.
So our algorithm is very simple, what we will do, we will scan the area, we will scan the area.
OK, so very simple.
So our court will look something like this.
I will start I will start scanning from zero till the last position and what I will check I will check
if the current value is equal to Gyi that we have to search, then yes.
See found found the key.
Found key and similarly, if the value of A becomes and and may also break here, OK, after we found
the key, we will break.
So if the value of my question, that means this Lopez terminated naturally.
So this will be terminated naturally.
That means the value of five becomes.
And so we can say gey not found.
OK, so we are just doing the linear scan, we are just doing the linear.
And each time I am comparing the value, if I got a match I am printing guess the key found and I'm
breaking the loop.
OK, and if the value of AI becomes and that means the slope is terminated, naturally this big statement
has not been executed and that means the key is not present in the array.
So that as I told.
So I have named this file as linear search NBP, so via name this file as linear search, because I
am searching, I am searching linearly, OK?
So I am doing searching linearly.
So that's why they call it linear search.
OK, so this is called Línea Search, Vilina Search, because I am searching linearly.
So the name of this file is lenience, that's not TBP, so first let us take in and let us take inpart.
Now, let us make any.
Now, let us take the values off at as import.
So what I am writing, I am writing a function and it will give me a position so I have a function,
the name of the function is linear, linear, such OK, so linear search function will take to this
function, will take at it.
And the size of that is input and it will return me the position at which the number is present.
The key is present when thing here.
We also need to take the key for which we have to sort something out.
And the key.
So let us make a variable in key and see in key.
OK, so we also need to pass key here, so, yes, it will take three arguments at the site of that.
And the key for which it will search and what function will return, the search function will return
medy position at which the given key is present.
So let's make the function so they don't end because we will retain the position name of the function
is Leanyer.
Search, and it is taking three arguments as input, so and at the size of the area and the third argument
or the third parameter is the key for which it will do search, it will perform a search operation.
So what I have to do, I have to do just a linear scan of the area so far and I equals zero.
I last and I placeless what I have to do, compare the current value, compare the current value, which
is elfy with the given key.
And if you got a match that means the given key is present in the area.
So you will return.
I guess I will return.
I, I miss.
So what I have to return I have to return the position at which the given case present so that given
his present position, I so I am returning a.
And if this loop got terminated, so if I reach line number 12, that means the given guy is not present
in the ETTY, then I can return.
I can return minus one.
OK, so if the gunky is not present in the area, what will happen, this return written statement will
never be executed.
So if this statement is never executed, this loop will get terminated naturally.
And if this loop gets terminated, naturally, that means the given guy is not present in the area and
I am returning minus one Y minus one because minus one is not a valid index.
You can also written minus 10, minus 20, minus three, minus four, minus five, minus hundred.
OK, so you just have to return an invalid index.
So minus one is an invalid index.
So I'm returning minus one.
So this is the position.
So first of all, I will check if the position so if the operation equals equals minus one, that means
the given key is not present in the eddy.
So you can see out.
Guinot found Gey not found.
OK, as we can safely say that the given key is present.
OK, so setout out key found at index position.
OK, so this is our entire call.
Let's see it again.
So leniencies function is taking three arguments, three barometer's as input.
I am doing a linear scan.
I am comparing the value of G with the current value.
If I got a match, I will return I OK, so this function will return any data that is position at which
the key is found.
And if this loop got terminated, naturally I am returning minus one.
Minus one means are invalid index.
That means key not found.
So it is my position I am taking if position equals minus one key not found otherwise key found at index
position.
So let's compile.
OK, so it will be D'Arte.
OK, it will be set in.
So let's say there are.
Five elements and the elements are one, two, three, four and five, so in Turkey, for example,
I want to search for three so lucky found at next to OK, so this is a net zero and one and next two
to three found at index to.
One more time later, number of elements are four and the elements are five, seven, eight and zero.
So I want to search for zero.
So we found at index three.
So this is our industry.
OK, indexing at a start from zero.
Now, last time.
Suppose MPs are five and the elements are one, two, three, four and five, and I want to search for
six.
So Kinnard found OK, Gey not found.
Now, one last thing that I want to clear.
OK, so, for example, if my is one, two and three and my key is when I guess what will happen, I
close the door, I listen and I placeless aof zero initially vilify zero zero eight klasky.
So one equals one, one equals one return I so I is zero zero one two.
So return I that is return zero.
OK, so as soon as this return statement is executed, this program is finished.
OK, this dysfunction is finished, if this statement is executed, I will directly return the value
and the program will not be executed.
So one more time.
For example, at Azz, one, two, three and four, and they were loaded, I want to search for it too.
So first of all I will check is when it goes to no is too close to yes.
OK, so what I will do, I will return a net is zero one, so I will return one.
So as soon as this statements get executed I will come here and the execution of basically this program
gets finished.
This function, I'll get finished.
OK, and in case, for example, if the key is not present, suppose there are two elements in the area,
one and two.
And the key that we have to search for is three.
So one is not close to three, two is not close to three.
So this loop will get terminated.
I will reach headline number 13.
Return minus one.
Minus one is an invalid index.
OK, you can also return minus two, minus three anything, any invalid index.
And before bringing the answer, I will check if I get an invalid index, that means King found otherwise,
I found the key at index below as so this is called linear search and how many steps it is taking.
So it is just performing a linear scan over the array.
So it is taking and the steps.
So linear search takes and steps, or you can see the time complexity of linear search is bigger off
and OK, so this is all about linear search.
So this is it for this video.
Thank you.