Skip to content
Educora
IntermediateGrades 8–1016 min27 / 59

Searching algorithms

Learn how linear and binary search work, compare their speed and see why binary search needs a sorted list.

Check yourself
In this lesson you will learn
  • Explain and apply linear search
  • Carry out binary search on a sorted list step by step
  • Estimate how many comparisons each method needs

Murad thinks of a number from 1 to 100, and Aysel has to find it. After each guess, Murad only says “higher”, “lower” or “correct”. If Aysel asks 1, 2, 3… in order, she might need 100 guesses. But if she starts at 50 and halves the range each time, she will find the number in at most 7 guesses. These two approaches are two famous algorithms: linear search and binary search.

Linear search

In linear (sequential) search, the items of a list are compared with the target one by one, starting from the first. When a match is found, the search stops; if we reach the end of the list without a match, the item is not there.

The advantage of linear search is that it is simple and works on any list, even an unsorted one. The drawback is speed: in the worst case, a list of n items needs n comparisons.

Example: linear search

Use linear search to find 23 in the list [7, 3, 23, 9, 15].

Show solution
Comparison 1: 7 ≠ 23 → keep going.
Comparison 2: 3 ≠ 23 → keep going.
Comparison 3: 23 = 23 → found!
The item is in 3rd place (in programming, numbering starts at 0, so its index is 2). It took 3 comparisons.
Python
def linear_search(items, target):
    for i in range(len(items)):
        if items[i] == target:
            return i
    return -1

numbers = [7, 3, 23, 9, 15]
print(linear_search(numbers, 23))
print(linear_search(numbers, 4))
▸ Expected output
2
-1
The function returns the index of the item, or -1 if the item is not in the list.
Interactive
Loading simulation…
Follow linear search step by step: only one item is checked at each step.

Binary search

Binary search works only on a sorted list (for example, in increasing order), but it is very fast. At each step the target is compared with the middle item of the range, and half of the list is thrown away at once.

  1. 1
    Find the middle

    Take the item in the middle of the current search range.

  2. 2
    Compare

    If the middle item equals the target, it is found and the search is over.

  3. 3
    Drop half

    If the target is smaller than the middle item, keep searching in the left half; if it is bigger, in the right half.

  4. 4
    Repeat

    Repeat steps 1–3 until the item is found or the range is empty. An empty range means the item is not in the list.

Example: binary search

Use binary search to find 37 in the sorted list [3, 8, 12, 17, 23, 31, 37, 42, 50]. The indexes of the items are 0 to 8.

Show solution
Step 1: range 0–8, middle index (0 + 8) ÷ 2 = 4, item 23. 37 > 23 → keep the right half: indexes 5–8.
Step 2: middle index (5 + 8) ÷ 2 = 6 (whole part), item 37. 37 = 37 → found!
It took only 2 comparisons. Linear search would have needed 7.
Interactive
Loading simulation…
Watch how the search range is cut in half at every step of binary search.
Python
def binary_search(items, target):
    low, high = 0, len(items) - 1
    steps = 0
    while low <= high:
        mid = (low + high) // 2
        steps += 1
        if items[mid] == target:
            return mid, steps
        elif items[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1, steps

numbers = [3, 8, 12, 17, 23, 31, 37, 42, 50]
print(binary_search(numbers, 37))
print(binary_search(numbers, 5))
▸ Expected output
(6, 2)
(-1, 3)
The function returns the index and the number of comparisons. The number 5 is not in the list: after 3 steps the range is empty.

Which one is faster?

In binary search every comparison halves the list, so doubling the list adds just one more comparison. For large lists the difference is amazing:

Number of itemsLinear searchBinary search
10104
1001007
1000100010
1,000,0001,000,00020
Number of comparisons in the worst case

Key points

  • Linear search checks items one by one and works on any list.
  • On a list of n items, linear search needs up to n comparisons.
  • Binary search works only on a sorted list and halves the range at every step.
  • Binary search needs at most 10 comparisons for 1000 items and 20 for a million.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
What does binary search require?