Searching
Searching
Linear Search is a simple searching algorithm used to find an element in a list or array by
checking each element one by one. It works for both sorted and unsorted data.
The algorithm for linear search is relatively simple. The procedure starts at the very first index of the
input array to be searched.
Step 1 − Start from the 0th index of the input array, compare the key value with the value present in
the 0th index.
Step 2 − If the value matches with the key, return the position at which the value was found.
Step 3 − If the value does not match with the key, compare the next element in the array.
Step 4 − Repeat Step 3 until there is a match found. Return the position at which the match was
found.
Step 5 − If it is an unsuccessful search, print that the element is not present in the array and exit the
program.
Worst Case: O(n) → When the element is at the end or not present.
Space Complexity