Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Linear search and binary search

After this lesson: I can compare linear search and binary search by the steps they take.

60 min lesson4 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

Finding one order among a million

A search algorithm finds one item in a collection, and the two classic ones are linear search and binary search. Knowing both lets you choose the one that stays fast when the data grows from a thousand rows to a million.

A web shop looks up an order number every time a customer asks where their parcel is. One method reads every order. The other reads about twenty.

By the end of this lesson

Note

I can compare linear search and binary search by the steps they take.

Key words

WordWhat it meansExample
Linear searchlook at each item in turn until you find it
Binary searchhalve a sorted list again and again
Sortedin order, smallest first2, 5, 8, 12
Indexthe position of an item, counting from 0
Middlethe item halfway between low and high
Worst casethe input that needs the most steps

The cells students fill in are left blank in this preview.

Linear search checks every item in turn

Linear search starts at the first item and compares each one with the target until it finds it or runs out.

It works on any list, sorted or not. In the worst case the target is last or missing, and the search takes n steps. That is linear growth.

Run a linear search and count

Code: python
# Linear search: look at the items one by one.
orders = [31, 12, 47, 8, 25, 60, 3, 19]
target = 60
steps = 0
position = -1
for i in range(len(orders)):
    steps = steps + 1
    if orders[i] == target:
        position = i
        break
print("found at", position, "after", steps, "steps")
# Change target to 31, then to 99, and run again.

A line-by-line search in the terminal

The command grep is a linear search over lines. With -n it prints the line number, and with -m1 it stops at the first match.

The Run below writes a million numbers and searches for 750000. The number before the colon is the line where grep stopped, so grep read that many lines.

Search a million lines

Command
seq 1 1000000 | grep -n -m1 '^750000$'

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Binary search halves a sorted list

Binary search compares the target with the middle item. If the target is bigger, the lower half cannot hold it, so you throw that half away. If smaller, you throw away the upper half.

Then you repeat on what is left. Each step halves the list, so 1000 items need at most 10 steps.

Worked example: find 23 in ten items

StepLowHighMiddle itemWhat happens
1091616 is smaller than 23, so low moves to 5
2595656 is bigger than 23, so high moves to 6
35623Found at index 5

The cells students fill in are left blank in this preview.

Run a binary search and count

Code: python
# Binary search: the list must be sorted.
items = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]
target = 23
low = 0
high = len(items) - 1
steps = 0
position = -1
while low <= high:
    middle = (low + high) // 2
    steps = steps + 1
    if items[middle] == target:
        position = middle
        break
    if items[middle] < target:
        low = middle + 1
    else:
        high = middle - 1
print("found at", position, "after", steps, "steps")
# Change target to 91, then to 40, and run again.

A million items need about twenty steps

Doubling the list adds only one step, because one more halving covers it. So the steps grow like the number of times you can halve n. That is logarithmic growth.

Python's bisect_left is a ready-made binary search. The Runs below ask it for a position and count the halvings of a million.

Binary search a million numbers

Command
python3 -c "import bisect; print(bisect.bisect_left(range(1, 1000001), 750000))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Halvings needed for a million

Command
python3 -c "import math; print(math.ceil(math.log2(1000000)))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Binary search only works on a sorted list

The method relies on the order. When it throws half the list away, it assumes the target cannot be there.

On an unsorted list that assumption is false, and the search can miss an item that is present. Run the example, then check which method suits which job.

See binary search miss an item

Code: python
# The same method on a list that is NOT sorted.
items = [9, 2, 7, 4, 5]
target = 2
low = 0
high = len(items) - 1
position = -1
while low <= high:
    middle = (low + high) // 2
    if items[middle] == target:
        position = middle
        break
    if items[middle] < target:
        low = middle + 1
    else:
        high = middle - 1
print("2 is really at position 1, the search says", position)

Sort three numbers first

Command
printf '3\n1\n2\n' | sort -n

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think binary search is always the faster choice. It is only correct on a sorted list, and sorting costs steps too. For one search on an unsorted list, linear search is the right tool. Use binary search when you search the same sorted data many times.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.