What is the difference between Linear search and Binary search?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Linear search and binary search both answer the same question, “is this value present,” but they rely on very different assumptions. Linear search works on almost any list because it checks items one by one. Binary search is much faster on large inputs, but only when the data is already sorted and supports efficient middle access.
How Linear Search Works
Linear search scans from one end of the collection until it finds the target or reaches the end.
This algorithm is simple and works on unsorted data. It does not need any preprocessing or special structure.
The tradeoff is that in the worst case it may inspect every element.
How Binary Search Works
Binary search repeatedly checks the middle element and discards half of the remaining search space each step.
This is much faster on large sorted arrays because each comparison eliminates half the remaining candidates.
The Biggest Difference: Sorted Data
The most important practical difference is not only speed. It is the precondition.
- Linear search works on unsorted data.
- Binary search requires sorted data.
If the data is not sorted, binary search is not merely slower. It is wrong.
That is why binary search is powerful but less universally applicable.
Time Complexity
The usual complexity comparison is:
- linear search:
O(n) - binary search:
O(log n)
That difference becomes large quickly. Searching one million sorted elements with binary search takes only a small number of comparisons compared with potentially one million checks for linear search.
But complexity is not the whole story. If the data must first be sorted just to perform one search, the total cost may outweigh the benefit.
Data Structure Matters Too
Binary search assumes efficient random access to the middle element. Arrays and vectors are a natural fit. Linked lists are not.
Linear search works fine on sequences where you naturally move one element at a time.
So the real decision is influenced by:
- whether the data is sorted
- whether you will search many times or only once
- whether the structure supports fast indexing
When Linear Search Is the Better Choice
Linear search is usually the better choice when:
- the collection is small
- the data is unsorted
- you only need one or a few searches
- the structure does not support efficient indexing
Its simplicity is a real advantage. For tiny lists, the complexity difference may not matter enough to justify sorting or extra logic.
When Binary Search Is the Better Choice
Binary search is usually the better choice when:
- the data is already sorted
- the collection is large
- many search queries will be performed
- random access is cheap
That is why binary search appears so often in libraries and systems code built around sorted arrays or lookup tables.
Sorting Changes the Tradeoff
Suppose you receive an unsorted list and need to check membership once. Linear search is often the better answer because binary search would first require sorting.
If you need to search the same dataset thousands of times, sorting once and then using binary search can be much faster overall.
This is the operational difference many beginner explanations leave out.
Common Pitfalls
- Using binary search on unsorted data.
- Comparing
O(n)andO(log n)without considering sorting cost. - Assuming binary search is always the better answer just because it is faster asymptotically.
- Applying binary search to a structure that does not support efficient middle access.
- Forgetting that for very small inputs, the simpler algorithm may be perfectly adequate.
Summary
- Linear search checks items one by one and works on unsorted data.
- Binary search repeatedly cuts the search space in half but requires sorted data.
- Linear search is
O(n)and binary search isO(log n). - Binary search is best for large sorted collections with repeated lookups.
- The right choice depends on the data shape and the actual workload, not only on the complexity table.
Related reading
- What is the difference between O, Ω, and Θ?
- What is the difference between partial fit and warm start?
- What is the difference between Q-learning and Value Iteration?
- What is the difference between sortedlist vs list.sort?
- What is the difference between List of T and Collectionof T?
- What is the difference between List.of and Arrays.asList?
- What is the difference between stdsort and stdstable_sort?
- What is the difference between Travelling Salesman and finding Shortest Path?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.