Hey, guys, what's up?
So in this video, we will learn how to search for a given key in an area.
Okay, so what will happen?
We will be given an area.
So we have an area.
And later, the elements are 10, 20.
Three, four.
One zero.
OK.
So we have an array and let's say we have to search for 20.
OK.
So we have to search for a given value in Eddie.
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 is present.
So our vote will be one.
OK.
One means index that is 10 days present at index one.
So, for example, if the key that we have to search for is a party today is not present.
So now it should be not present.
Or you can say key not found.
Key not found.
And here we have found key.
At edition.
When.
OK.
So you never give us an error and you never gave us.
For the value that we have to search in the area.
And if the given key is present in the area, you have to print that.
Yes.
The given key is present in that.
And it is present at position.
When 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 against all this problem.
So the algorithm for solving this problem is very simple.
What we will do.
They will scan that.
We will scan Eddie from left to right.
OK.
So starting from zero to end, minus one.
We will scan the eddy, 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 two.
So what we will do.
I will check.
Is five for close to No.
Is 40 close to.
No.
Is too close to.
Yes.
Too close to stop.
So ultimately, what we are doing, we are scanning the area from left to right, from zero to index
and minus one.
Similarly, if we have to search for, let's say zero.
So is 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 at 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.
No is four equals six.
No.
Is two equals six.
No.
Is one.
Equals six.
No.
Is three equals six.
No.
Is zero.
Equals six.
No.
So I will reach here.
OK.
So I will reach everybody.
Shannon.
That means gay sex is not present.
Physics is not present in the Eddie.
So our algorithm is very simple.
What we will do.
We will scan the area.
We will Scandi Eddie.
Okay.
So very simple.
So our Corbel 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 Gey that we have to search, then yes.
See out.
Found.
Found the key.
Found key and similarly, if the value of AI becomes an end, we also break.
OK.
After we found the key, we will break.
So if the value of AI close N, that means this Lopez terminated naturally.
So this Lubitch terminated.
Naturally, that means the value of AI becomes.
And so we can say gey not found.
OK.
So we are just doing the linear scan.
We are just doing the linear scan.
And each time I am comparing the value.
If I got a match.
I am printing as the key found and I'm breaking the loop.
OK.
And if the value of AI becomes n.
That means this loop is terminated naturally.
This big statement has not been executed.
And that means the key is not present in the area.
So let the slide called.
So I have named this file as linear, so it's not TPP.
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 Wiliness Search because I am searching linearly.
So the name of this vial is Lenience Usdaw TBP.
So first let us take it and let us take input.
Now let us make any.
Now, let us take the values off areas input.
So what I am writing, I am writing a function and it will give me position.
So I have a function.
The name of the function is linear, linear search.
OK, so linear search function will take two linear sites.
Function will take Eddie.
And the size of that is input.
And it will return me the position at which the number is present.
The key is present.
Well, moulting here.
We also need to take the key for which we have to search Soucy out.
And the key.
So let us make a variable in the key and see in key.
OK.
So we also need to pass key here.
So Leena's, it will take three arguments.
Eddie, the size of dairy.
And the key for which it will search and word linear function will return.
They Nessa's function will return midi position at which the given key is present.
So let's make the function so that we don't typist.
And because we will retain the position, name of the function is linear.
Search and it is taking three arguments as input.
So and Eddie, the size of the Eddie and the third argument or the third about amateur's the key for
which it will do search it will perform 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 less than an A plus plus what I have to do.
Compare the current value.
Compare the current value, which is zero phi with the given key.
And if you've got a match, that means the given key is present in the area.
So you will return I guess.
Or you will return.
I, I means.
So what I have to done, I have to return the position at which the given case present, so the given
case, present and position I so I am returning I.
And if this loop got terminated.
So if I reach line number 10, that means the given key is not present in the eddy.
Then I can return.
I can return minus one.
OK, so if the given key is not present in the area, what will happen, this written ICE statement
will never be executed.
So if this statement is never executed, this loop will get terminated naturally.
And if this loop gets damaged naturally, that means the given key is not present in the area.
And I'm returning minus one Y minus one Magos minus one is nota valid index.
You can also return minus 10, minus two and B 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 politician.
So first of all, I will check if the position.
So if the position equals equals minus one, that means the given key is not present in the EDDI.
So you can see out.
Keen found, key not found.
OK, as we can safely say that the given key is present.
OK, so see out key fund and index perdition.
OK, so this is our entire call.
Let's see it again.
So lenience, its function is taking three arguments, three barometer's as input.
I am doing a linear scan.
I am conveying the value of key 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 that key is found.
And if this loop got damaged, naturally I am returning minus one.
Minus one means are invalid index.
That means key not found.
So it is my position.
I am checking if potion equals minus one key.
Not found otherwise.
Key found at index position.
So let's compile.
OK, so it will be darte.
OK, it will be see in.
So let's say that our.
Five elements and the elements are one, two, three, four and five.
So and Turkey, for example, I went to search for three.
So the key found at next to.
OK, so this is a neck zero in the X1 and X2.
So three found at index to.
One more time led to a number of elements are formed and the elements are five, seven, eight and zero.
So Angeliki, I want to search for zero.
So we found at index three.
So this is at index three.
OK, indexing at a start from zero.
Now, last time.
Suppose a number of elements are five and the elements are one, two, three, four and five.
And I want to search for six.
So key not found.
Okay.
Key not found.
Now, one last thing that I want to clear.
OK, so, for example, if my airways one, two and three and my key is one, OK?
So what will happen equals zero.
Less than an A plus plus of zero initially.
Well if I zero eight zero eight koskie.
So one equals one.
One equals one return I.
So I is zero zero one two.
So return eight.
That is return zero.
OK.
So as soon as this return statement is executed, this program is finished.
OK.
This this function is finished.
If this written statement is executed, I regret it.
Later it the venue and the program will not be executed.
So one more time.
For example, adays one, two, three and four.
And the value that I want to search for is two.
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 statement get executed, I will come here.
And the execution of basically this program gets finished.
This function get finished.
OK.
And in case, for example, of the given case not present, suppose there are two elements in the area.
One and two.
And the key that we have to search for history.
So one is not to close to three.
Two is not to close to three.
So this loop will get terminated.
I will reach at line number 13.
Return minus one.
Minus one is an invalid index.
OK.
You can also read minus two, minus three.
Anything, any invented index.
And before printing answer, I will check if I get an invalid index.
That means Keehner found otherwise.
I found the key at index B.
U.
S.
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, Lina, search takes an steps, or you can say that time complexity of linear search is bigger off
and.
OK.
So this is all about linear search.
So this isn't for this video.
Thank you.