linear search
binary search
search algorithms
algorithm comparison
data structures

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.

Practice algorithms

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.

python
1def linear_search(values, target):
2    for i, value in enumerate(values):
3        if value == target:
4            return i
5    return -1
6
7print(linear_search([7, 3, 9, 1], 9))

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.

python
1def binary_search(values, target):
2    lo = 0
3    hi = len(values) - 1
4
5    while lo <= hi:
6        mid = lo + (hi - lo) // 2
7        if values[mid] == target:
8            return mid
9        if values[mid] < target:
10            lo = mid + 1
11        else:
12            hi = mid - 1
13
14    return -1
15
16print(binary_search([1, 3, 7, 9, 12], 9))

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) and O(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 is O(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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.