![]() |
VOOZH | about |
Linear Search is the simplest searching algorithm that checks each element sequentially until a match is found. It is good for unsorted arrays and small datasets.
Given an array a[] of n elements, write a function to search for a given element x in a[] and return the index of the element where it is present. If the element is not present in the array, then return -1.
Input/Output:
Input: a = [ 1, 2, 3, 5, 7], x = 3
Output = Element found at index: 2Input a = [1, 2, 3, 5, 7] x = 8
Output = -1
Please refer to the complete article on Linear Search for more details.
Example:
Element found at position 1
Time Complexity:
Space Complexity: O(1)