Problem: Given an array arr of n elements, write a function to search a given element x in arr.
A simple approach is to do linear search, i.e
- Start from the leftmost element of arr and one by one compare x with each element of arr
- If x matches with an element, return the index.
- If x doesn’t match with any of elements, return -1.
C and C++
The time complexity of above algorithm is O(n).
Linear search is rarely used practically because other search algorithms such as the binary search algorithm and hash tables allow significantly faster searching comparison to Linear search.