Reading Time:- 2 min 05 sec
Reading Time:- 2 min 05 sec
#linear-search #sequential-search #DSA #linear-search-techniqueBY TEJAS
The searching technique is a computer operation whose job is to find an element from its location.
There are two main types of searching techniques
So in this article, we will learn about linear search and also see its example. This will be very simple for you to understand.
ADVERTISEMENT
Linear search is also known as sequential search.
Linear search is a much simpler search method than binary search.
The element that is to be found in a linear search method is searched linearly or sequentially (line to line) this searching method is called the linear search method.
Linear search works on both sorted or unsorted lists.
If we look at what is a linear search in simple language, it is a searching method technique in which an element is searched line by line in that list or array.
So let us see a simple example of this technique.
ADVERTISEMENT
Consider, we have one of the following arrays which are unsorted.
int array [] = {25, 84, 32, 70, 61}
From that, we want to find element 70
which is at the second last position.
So what exactly happens in this search technique?
All your elements are stored in an array. As we have seen in the array above.
zero
element in the array. If, both the elements are equal then searching will stop there & the position at which the value is located will be printed & the program will stop.
zero
position, then the same element will be compared with the element in the first
position, and if we don't find that element here, then it will be compared with the element in the second
position.
Let us see step by step example.
int array [] = {25, 84, 32, 70, 61}
We want to find the third
element of this whichever is 70
.
ADVERTISEMENT
Step 1
Step 1
Comparison between:
0^{th} position element is 25
and search element is 70
.
Conclusion: (25!=70
) 25 is not equal to 70
Result: Compare Next element.
ADVERTISEMENT
Step 2
Step 2
Comparison between:
1^{st} position element is 84
and search element is 70
.
Conclusion: (84!=70
) 84 is not equal to 70
Result: Compare Next element.
ADVERTISEMENT
Step 3
Step 3
Comparison between:
2^{nd} position element is 32
and search element is 70
.
Conclusion: (32!=70
) 32 is not equal to 70
Result: Compare Next element.
ADVERTISEMENT
Step 4
Step 4
Comparison between:
3^{rd} position element is 70
and search element is 70
.
Conclusion: (70==70
) 70 is equal to 70
Result: Print the element and position nd stop the search.
Now that we have found our element, we no longer need to continue the searching process, so we will stop the searching process here.
But if we had not found our element in step 4, then we would have continued this searching process.
COMMENTS