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 list2 REPEAT3 Check the middle item of what is left4 IF middle item = target THEN stop: found it5 IF target < middle item THEN bin the middle item and everything above it6 IF target > middle item THEN bin the middle item and everything below it7 UNTIL target found OR nothing is left
Variables
Output
Step 1: Here's the list: 15 numbers, smallest to biggest. We want 45. No checks yet. Where would you look first?
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.
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.
Ready when you are — step through one line at a time.
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.
Walk the route that matches your list. For any one list, only one route is true.
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
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?
How do we compare how efficient two searches are?
How does linear search work?
Does linear search need a sorted list?
What must be true of a list before binary search can be used?
Binary search: what happens at each check?
When does binary search stop?
In a binary search the target is smaller than the middle item. What gets binned?
Roughly how much of the remaining list does each binary search check rule out?
Linear search: best and worst case?
A sorted list doubles in length. What happens to each search's worst case?
Why might sorting a list first not be worth it?
When can linear search be the better choice?
When is binary search the better choice?
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 postedMore KS3 Computer Science topics
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.