Overview
Linear Search is the simplest search algorithm. It scans the array from the first element to the last, comparing each value against the target, and stops as soon as it finds a match.
Its great advantage is generality: it needs no ordering or preprocessing and works on any list, including unsorted data and linked structures. That flexibility comes at the cost of speed on large inputs.
How Linear Search works
- Start at the first index of the array.
- Compare the current element with the target value.
- If they match, return the current index; the search is done.
- Otherwise move one position to the right and repeat until the end of the array.
- If the end is reached with no match, report that the target is absent.
When to use it
- Searching small or unsorted arrays where no ordering can be assumed.
- One-off lookups where the cost of sorting first would outweigh a single scan.
- Sequential data such as linked lists or streams that lack random access.
Complexity analysis
Linear Search runs in O(n) time in the average and worst cases, since it may inspect every element before finding the target or concluding it is absent. The best case is O(1) when the match is at the first position. It uses O(1) extra space and requires no sorting.
Frequently asked questions
When should I use Linear Search instead of Binary Search?
Use Linear Search when the array is small, unsorted, or when you would search only once — sorting the data just to run a faster search often costs more than a single O(n) scan.
Does Linear Search need a sorted array?
No. It compares elements one by one regardless of order, which is exactly why it is the go-to method for unsorted collections.