Linear search is a very basic and simple search algorithm. In Linear search, we search an element or value in a given array by traversing the array from the starting, till the desired element or value is found.
Time complexity of Linear search algorithm is O(n), we will analyze the same and see why it is
O(n) after implementing it. Here is an image of Linear Search:
Linear Search: Steps on how it works:
Here is simple approach is to do Linear Search:
- Start from the leftmost element of array and one by one compare the element we are searching for with each element of the array.
- If there is a match between the element we are searching for and an element of the array, return the index.
- If there is no match between the element we are searching for and an element of the array, return -1.