KS3 · Computer Science

Linear vs binary search

Looking up 'penguin' in a dictionary, you don't start at page one. You open it near the middle, see where you've landed, and jump. You already know a search algorithm.

Computing · Searching algorithms

Binary search: check the middle, bin half

A sorted list of 15 numbers, and we're hunting for 45. Step through and watch the list shrink. A dot (·) is an item that's been thrown away.

Before you press Next: guess how many checks it will take to find 45. Write your guess down. Seriously.

1 Start with the whole sorted list
2 REPEAT
3 Check the middle item of what is left
4 IF middle item = target THEN stop: found it
5 IF target < middle item THEN bin the middle item and everything above it
6 IF target > middle item THEN bin the middle item and everything below it
7 UNTIL target found OR nothing is left

Variables

checks=0target=45middle item=—still in play=3 7 11 15 19 24 28 32 37 41 45 50 56 61 68

Output

 

Step 1: Here's the list: 15 numbers, smallest to biggest. We want 45. No checks yet. Where would you look first?

1 / 9

Every stretch of list in this search has an odd number of items (15, then 7, then 3, then 1), so there's always exactly one middle item. The values are prewritten: nothing is being run.

Step 1 of 9: Here's the list: 15 numbers, smallest to biggest. We want 45. No checks yet. Where would you look first?.

Computer Science · Algorithms

Trace table — dry run the code

Same list, same target, the other way: start at the front and check every item in turn. Keep your eye on the checks column.

1Start at the first item
2REPEAT
3 Check this item
4 IF item = target THEN stop: found it
5 Move on to the next item
6UNTIL target found OR the end of the list is reached
position
item
is it 45?
checks
Output
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Computing · How many checks?

Now make the list much longer

A sorted list has 1,000 items. At most, how many checks could a binary search ever need, whether it finds the target or ends up sure it isn't there?

Your estimate

500 checks

0 checks1000 checks

Two algorithms, one job: linear vs binary

Linear searchvsBinary search

Start with the top row. It's the one people forget.

Focus

Does the list need to be sorted?

Linear search

No. Any list, sorted or jumbled.

Binary search

Yes. The list must already be sorted (smallest to biggest, or A to Z).

The insight

Binary search decides which half to bin by comparing the target with the middle item. That only works if smaller items sit on one side of the middle and bigger ones on the other. On a jumbled list, the target could be sitting in the half it just binned.

How does it check items?

Linear search

One after another, starting from the first item.

Binary search

Checks the middle of what's left, then bins the middle and the side that can't hold the target.

Worst case as the list gets longer

Linear search

Has to check every item, so the checks grow in step with the length. Double the list, double the worst case.

Binary search

Each check bins about half, so doubling the list adds only about one more check.

How simple is it to write?

Linear search

Very simple: start at the front and keep going.

Binary search

More to keep track of: where the middle is, and which part is still in play.

Computing · Choosing a search

So which search should you use?

Choose a branch at each level to reach a decision.

Is the list sorted? → What kind of job is it?

4 situations.

On You need to find an item in a list. 2 branches to choose from.

Walk the route that matches your list. For any one list, only one route is true.

Computing · Check your thinking

What do you really think binary search is?

Be honest: which of these is closest to what you think right now?
How sure are you?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Two ways to find something in a list, and why one of them barely slows down when the list doubles

What you need to know

  • A search looks through a list to find whether a target item is there and, if it is, where.
  • To compare searches, count the comparisons: how many items each checks before finding the target or knowing it isn't there.
  • Linear search starts at the first item and checks each one in turn until it finds the target or reaches the end.
  • Have a goA linear search hunts for 8 in the list 5, 8, 2, 9. How many checks does it make?

    2: it checks 5, then 8, and stops.

    It stops the moment it finds the target, so it never looks at 2 or 9, and the check that finds 8 counts too.

  • Linear search works on any list, sorted or unsorted. Best case: one check. Worst case: every single item.
  • Binary search only works on a list that's already sorted, and it starts by checking the middle item.
  • Not the target? Bin the middle item and the half that can't hold the target, then repeat on what's left.
  • Have a goMax is binary-searching 2, 4, 6, 8, 10, 12, 14 for 12. He checks 8, then proudly bins 10, 12 and 14. What would you tell Max?

    Wrong half, Max! Bin 8 and everything below it; keep 10, 12, 14.

    12 is bigger than 8, so it can only be above 8. Max binned the only half that could hold his target.

  • Binary search stops when it finds the target or when nothing is left, so an empty list takes zero checks.
  • Double a list's length: linear search's worst case doubles, but binary search needs only about one more check.
  • Sorting takes extra work. Short or unsorted list, searched once? Linear. Big sorted list, searched often? Binary.
  • Have a goA binary search needs at most 7 checks on a sorted list of 64 items. Double it to 128 items. At most how many checks now?

    8

    The first check halves 128 back down to about 64, which needs 7 more at most. Guessing 14 treats binary search like linear search.

The big picture

Two algorithms, one job: find a target in a list, or show it isn't there. Linear search checks every item in turn from the start, and it works on any list. Binary search only works on a sorted list: check the middle item, bin the middle and the half that can't hold the target, repeat. Count the comparisons and binary search wins easily on long sorted lists, because doubling the list adds only about one check. But sorting first costs work, so for a short or unsorted list searched once, linear search can be the smarter pick.

Key points

1A search finds whether a target is in a list and where; compare searches by counting comparisons.
2Linear search checks every item in turn from the start and works on any list.
3Binary search needs a sorted list: check the middle, bin the middle and the half that can't hold the target, repeat until found or nothing is left.
4Linear search's worst case grows in step with the list; doubling the list adds only about one check to binary search.
5Sorting costs extra work, so the best search depends on the list and how often it's searched.

Worked example

Problem

Use binary search to look for 20 in this sorted list: 4, 9, 13, 18, 22, 27, 31. Which items get checked, and what does the search find?

⚠ Watch out

Thinking binary search works on any list. It bins half the list based on the middle item, which only makes sense when the list is sorted. On a jumbled list it can throw away the very half the target is in. Close second: not counting the final check, the one that finds the target.

🧠

Memory hook

Linear walks, binary halves. A walker visits every door; a halver bins half the street each time, but only if the street is in order.

✓

Check yourself

Without looking back: why does binary search go wrong on an unsorted list, and what happens to its worst-case number of checks when a sorted list doubles in length?

Flashcards

(14)
What does a search algorithm do?
Looks through a list to find whether a target item is there and, if so, where it is.
How do we compare how efficient two searches are?
Count the comparisons: how many items each one checks before it finds the target or knows it isn't there.
How does linear search work?
Start at the first item and check each item in turn until the target is found or the end of the list is reached.
Does linear search need a sorted list?
No. It works on any list, sorted or unsorted.
What must be true of a list before binary search can be used?
It must already be sorted.
Binary search: what happens at each check?
Check the middle item. If it's the target, stop. If not, bin the middle item and the half that can't hold the target, and repeat on what's left.
When does binary search stop?
When it finds the target, or when no items are left (so the target isn't there).
In a binary search the target is smaller than the middle item. What gets binned?
The middle item and the upper half, because the target can only be in the lower half.
Roughly how much of the remaining list does each binary search check rule out?
About half of it: the middle item and the half that can't hold the target.
Linear search: best and worst case?
Best: 1 check (the target is first). Worst: every item (the target is last or missing).
A sorted list doubles in length. What happens to each search's worst case?
Linear search's worst case doubles. Binary search needs only about one more check.
Why might sorting a list first not be worth it?
Sorting takes extra work. For a short list, or an unsorted list searched only once, linear search can be the better choice.
When can linear search be the better choice?
For a short list, or an unsorted list searched only once: no sorting needed, and it's simple to write.
When is binary search the better choice?
For a large sorted list that is searched often.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More KS3 Computer Science topics

See the full KS3 Computer Science curriculum →

How this lesson was checked. This KS3 Computer Sciencelesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 27 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.

Helpful guides for parents and students