Skip to content

Linear and binary search

Write linear search and binary search, tracing binary search's low, mid and high on a sorted list and counting comparisons, up to n against about log2 n.

It opens on

A sorted list holds 10 numbers, and 50 is not one of them. Checking from the front, how many of the 10 must you look at to be sure 50 is missing?

Why it matters

Searching is one of the most common things programs do, and binary search is the first algorithm where a smart idea turns a million steps into twenty; tracing low, mid and high and counting comparisons are standard exam questions.

Good to know first

  • While loops: a loop with a condition that changes
  • Loop patterns: return from inside a loop when the item is found
  • Lists: indexing and mutation: len and xs[i]

Step by step

  1. Linear search

    Write linear search returning the index or −1, and count its comparisons in the best and worst case.

    Here is nums, 10 sorted numbers. Searching for 56 compares 4, 9, 15, 23, 31, 42 and then 56, which matches at index 6.

    nums

    A common mix-up

    Linear search on a sorted list is fast because the list is sorted.

    In fact: This linear_search never uses the order; it still checks up to every item.

  2. Binary search traced

    Write binary search with low, mid and high, and trace it on nums for a target that is there and one that is not.

    Searching for 56, low is 0 and high is 9, so mid is 4, and nums[4] is 31. 31 is less than 56, so low becomes 5.

    searching for 56

    A common mix-up

    Mid is the middle value, like the average of the numbers.

    In fact: Mid is the middle index, (low + high) // 2, and the value there is xs[mid].

  3. Counting comparisons

    Count binary search's worst-case comparisons as about log2 n and compare them with linear search's n.

    The run measured the worst case, the most comparisons over many targets, as 4 for 10 items and 10 for a thousand. Linear search's worst case is the whole list.

    A common mix-up

    Binary search is twice as fast as linear search because it halves the list.

    In fact: It halves the list at every step, so it takes about log2 n steps, 20 instead of a million, not half a million.

Try it

How many comparisons does linear search make for 91, and for 4?Answer

10 and 1

Hint: Where are they in nums?

Trace a search for 15 in nums. Give each mid and the result.Answer

Mids 4 (31, go left), 1 (9, go right), 2 (15, found); returns 2

Hint: High becomes 3 after the first step

About how many comparisons does binary search need in the worst case for 4000 items?Answer

About 12, since 2 to the 12 is 4096

Hint: 1000 needs 10, and each doubling adds 1

How many comparisons did that take, and how many would a search for 50 take?Answer

7 for 56; 10 for 50, since 50 is not there and every item must be checked.

With low 5 and high 9, what is mid, what is there, and which way do we go?Answer

Mid is 7, nums[7] is 67, bigger than 56, so high becomes 6.

Linear and binary search

30 minutes every month. No card.