KS3 · Computer Science
Linear search
Hunting for your mate in a jumbled queue? Start at the front, check each face, stop when you spot them. Nobody left to check? Only then are they not there.
Computer Science · Algorithms
Trace table — dry run the code
One list, two searches. First we hunt for Mo, then for Sam. Step through one comparison at a time and watch how differently they end.
Ready when you are — step through one line at a time.
Predict, then check
Think about the exact moment the search stops.
A linear search looks for 5 in the list 8, 5, 12, 5, 3. There are two 5s. Which position does it report?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Check one item at a time until you find it, or run out of list.
What you need to know
- A search answers two questions: is the target in the list, and if so, where? We count positions from 1.
- Linear search starts at the very first item and compares it with the target.
- Match? The search stops right there and reports that position.
- No match? It moves on to the next item and compares again.
Have a goLinear search for 7 in the list 4, 7, 1. How many comparisons does it make before it stops?
2
4 is checked (no match), then 7 matches at position 2 and the search stops. Saying 3 means checking the 1 too, but nothing after a match is checked.
- If the target appears twice, you get the first position, because the search has already stopped.
- It may say 'not in the list' only when no items are left to check. An empty list needs zero comparisons.
Have a goJas checks 3 names on an 8-name party list, doesn't see yours, and announces: 'You're not invited!' Time to panic?
Not yet: 5 names haven't been checked.
'Not in the list' is only allowed once no names are left to check. Jas gave up with five to go, and yours could be any of them.
- A bigger or smaller item tells linear search nothing. It never relies on order, so sorted or unsorted, it works.
- Worst case, the target is last or missing: n items means n comparisons.
Have a goList A has 50 items and list B has 100. The target is missing from both. How many times more comparisons does B need?
Twice as many: 100 compared with 50.
A missing target costs one comparison per item, so doubling the list doubles the work. It isn't 'one extra': every new item is one more check.
- So the work grows with the list. On a long sorted list, binary search is usually much faster.
- A trace gets one row per comparison: the position, the item there, the target, and whether it matched.
The big picture
Linear search looks for a target by checking the items one by one, starting from the first. If an item matches, it stops and reports that position. If no items are left to check and there's been no match, it reports that the target is not in the list, so an empty list needs zero comparisons. It works on sorted or unsorted lists. On a list of n items the worst case is n comparisons, and on a long sorted list binary search is usually much faster.
Key points
Worked example
Problem
A shop's stock list holds the codes 42, 17, 88, 5, 63, in no particular order. Use a linear search to look for 5. Record each comparison and say what the search reports.
⚠ Watch out
Saying 'not in the list' while items are still unchecked. In an unsorted list, a bigger item or a run of misses proves nothing, because the target could be next. Report it missing only when there are no items left to check; for an empty list, that's true straight away.
Memory hook
Look, compare, move on. Stop at the first match. No items left? Only then say 'not in the list', and an empty list gets there straight away.
Check yourself
Trace a linear search for 30 in 10, 40, 20, 50, then in an empty list. You should get four comparisons and 'not in the list', then zero comparisons and 'not in the list'.
Flashcards
(13)What does a searching algorithm find out?
Where does a linear search start?
In linear search, what happens at each step?
When can linear search report 'not in the list'?
The target appears twice in a list. Which position does linear search report?
In an unsorted list, linear search meets an item bigger than the target. Can it stop?
Does a list have to be sorted before a linear search?
What is the worst case for linear search on a list of n items?
A target is missing from a list of 200 items. How many comparisons?
Why can linear search be slow on a long list?
What does binary search need that linear search doesn't?
Long sorted list: which search is usually much faster?
What does each row of a linear search trace record?
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 28 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.