Menu
Coddy logo textTech

Linear Search

Last updated

Linear search (also called sequential search) is the simplest search algorithm: start at the first element and compare each one with the target until you find a match or run out of elements. It makes no assumptions about the data — the array can be unsorted, and the elements can be anything you can compare for equality.

The animation above highlights each comparison as the scan moves left to right and stops the moment the target appears. Its simplicity costs speed: in the worst case every element is checked, so it runs in O(n). When the data is sorted, binary search finds the same answer in O(log n) — and if you need sorted data first, see merge sort.

Time & space complexity

CaseComplexityNotes
Best caseO(1)The first element is the target.
Average caseO(n)On average half the elements are checked before a hit.
Worst caseO(n)The target is last — or not present at all.
SpaceO(1)Only the current index is kept.

Step by step

StepWhat happens
1Start at index 0, the first element of the array.
2Compare the current element with the target value.
3If they are equal, return the current index — found.
4Otherwise move one position to the right and repeat.
5If the end of the array is reached without a match, the target is not present (return -1).

Worked example

Searching for 5 in [7, 3, 9, 1, 5, 8, 2]:

ComparisonIndexElementResult
1077 ≠ 5 — keep scanning.
2133 ≠ 5 — keep scanning.
3299 ≠ 5 — keep scanning.
4311 ≠ 5 — keep scanning.
5455 = 5 — found at index 4.
Use it whenAvoid it when
The data is unsorted or constantly changingThe data is sorted — binary search is exponentially faster
The collection is small, so simplicity winsThe dataset is large and searched repeatedly
You only have sequential access (streams, linked lists)You can afford an index or hash table for O(1) lookups

Linear Search code

A clean, runnable Linear Search implementation in Python, JavaScript, Java, C++, C, Pseudocode. Pick a language, copy the code, or open it pre-loaded in the Coddy Playground.

Linear Search code in Python

Python
1def linear_search(a, target):2    # Scan left to right until the target appears3    for i in range(len(a)):4        if a[i] == target:5            return i6    return -17
8
9nums = [7, 3, 9, 1, 5, 8, 2]10print("Index of 5:", linear_search(nums, 5))11print("Index of 4:", linear_search(nums, 4))
Run this code in the Python Playground

Linear Search FAQ

What is the time complexity of linear search?
O(n) in the average and worst case — the scan may have to visit every element — and O(1) in the best case when the first element is the target. It uses O(1) extra space.
Does linear search need sorted data?
No — that's its main advantage. Linear search works on completely unsorted data because it checks every element for equality; order never matters. Binary search, by contrast, only works on sorted arrays.
When is linear search better than binary search?
When the data is unsorted and searched only once (sorting first would cost O(n log n)), when the collection is tiny, or when you only have sequential access such as a stream or a linked list. For repeated lookups on sorted arrays, binary search wins.
Is linear search the same as sequential search?
Yes — the two names describe the same algorithm: scan the elements in sequence until the target is found or the collection ends.
How many comparisons does linear search make on average?
If the target is present and equally likely to be anywhere, about n/2 comparisons on average; if the target is absent, exactly n. That linear growth is why it's called linear search.
Coddy programming languages illustration

Master algorithms with Coddy

GET STARTED