Linear search algorithm.
Given an array of items.
You want to find the given index of an element.
The linear search algorithm returns the index of the element, or -1 if the element is not found.
Linear search works by going through the array from index 0,....n-1
comparing array[index] to element.
If they match you return the index.
if the array elements are compared from index 0 to n-1 and no element matches the element.
you return -1.
Hence the pseudo code
Loop through array from index = 0, to n-1.
if array[index] = element
return index
return - 1
Time Complexity is O(n).
because we are looping through the array n times.
Space Complexity is O(1).
We are not creating any new variable and hence using constant space complexity
Top comments (0)