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

Preview

Linear and binary search

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

50 minutes

Guess my number

I think of a number from 1 to 100. You guess. I only say "higher" or "lower".

Would you guess 1, then 2, then 3? No. You would guess 50. If I say "higher", you would guess 75. Each guess throws away a big part of the numbers. Computers search this way too.

Key words

WordWhat it meansExample
AlgorithmA set of steps to solve a problem.
Linear searchLook at each item in turn, from the start, until you find it.
Binary searchLook at the middle item and throw away the half that cannot hold the target. Repeat.
TargetThe value you are looking for.
SortedIn order, such as smallest to largest or A to Z.
IndexThe position of an item in a list. The first is 0.
ComparisonChecking one value against another.is it equal to the target?
Best caseThe situation where the algorithm does the least work.
Worst caseThe situation where the algorithm does the most work.

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

Linear search: one by one

In a linear search you start at the first item. Is it the target? If not, go to the next. Keep going until you find it or reach the end.

It is simple. It works on any list, sorted or not.

Try it: linear search

Code: python
names = ["Mei", "Tom", "Aisha", "Sam", "Zara"]
target = "Sam"
position = 0
checks = 0
found_at = -1

while position < len(names) and found_at == -1:
    checks = checks + 1
    if names[position] == target:
        found_at = position
    position = position + 1

print("Found at index", found_at, "after", checks, "checks")

# Try the target "Mei" and then "Bob". How many checks each time?

Trace table: linear search for Sam

CheckIndexItemIs it Sam?
10MeiNo
21TomNo
32AishaNo
43SamYes: found at index 3

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

Quick check: linear search

Have a go

Quick check: linear search

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Binary search: halve the list

Binary search needs a sorted list. Look at the middle item. Is it the target? Then stop.

If the target is smaller, the answer can only be in the lower half. Throw away the upper half. If the target is larger, do the opposite. Repeat with what is left.

Worked example: binary search, found

StepSearch area (index)MiddleItem in the middleDecision
10 to 631515 is smaller than 27: use the upper part, 4 to 6
24 to 6527It is 27: found at index 5

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

Try it: binary search

Code: python
numbers = [3, 8, 12, 15, 21, 27, 40]
target = 27
low = 0
high = len(numbers) - 1
checks = 0
found_at = -1

while low <= high and found_at == -1:
    middle = (low + high) // 2
    checks = checks + 1
    if numbers[middle] == target:
        found_at = middle
    elif numbers[middle] < target:
        low = middle + 1
    else:
        high = middle - 1

print("Found at index", found_at, "after", checks, "checks")
# // divides and drops the decimal part. Try the target 10.

The lesson goes on

10 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.