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.

1list ← ["Kai", "Ben", "Zoe", "Mo", "Ada", "Raj"]
2FOR position ← 1 TO 6
3 IF list[position] = target THEN
4 OUTPUT "Found at position ", position
5 STOP
6 ENDIF
7ENDFOR
8OUTPUT "Not in the list"
position
item
target
match?
Output
Press Start to run the first line.
·

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?

Check the trace

Where does this trace go wrong?

Search the list 2, 5, 9, 4, 7 for the target 7. Record each comparison and the result.

Riley's trace — which line goes wrong?

Worst case

How much work is a missing target?

A list holds 500 names and the one you're after isn't there. How many comparisons does a linear search make before it can say 'not in the list'?

Your estimate

500 comparisons

0 comparisons1000 comparisons

Compare the searches

Linear search or binary search?

Tick which search each statement is true of: linear, binary, both, or neither.

  • A Linear search
  • B Binary search
  1. Finds whether a target is in a list, and where
  2. Leaves the list exactly as it was
  3. Works directly on an unsorted list
  4. Needs the list to be sorted first
  5. Uses the order of the items to rule some out without checking them
  6. Starts at the first item and moves along one at a time
  7. On a long list, may have to check every single item
  8. Usually much faster on a long sorted list
  9. Puts the items into order

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

1Two ways to finish: find a match and stop, or run out of items with no match. An empty list runs out straight away.
2If the target appears more than once, linear search reports the first match. Later items are never checked.
3A bigger or smaller item tells linear search nothing about what comes next.
4A missing target in a list of n items takes n comparisons, including 0 comparisons when n = 0.
5A trace records every comparison: the position, the item there, and whether it matched.

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?
Whether a target item is in a list and, if it is, its position.
Where does a linear search start?
At the first item (position 1). An empty list has no first item, so there's nothing to compare.
In linear search, what happens at each step?
The current item is compared with the target. Match: stop and report the position. No match: move on to the next item.
When can linear search report 'not in the list'?
Only when no items are left unchecked. On an empty list, that's immediately.
The target appears twice in a list. Which position does linear search report?
The first one. It stops at the first match, so it never reaches the second.
In an unsorted list, linear search meets an item bigger than the target. Can it stop?
No. A bigger item says nothing about what comes next, so it moves on.
Does a list have to be sorted before a linear search?
No. It works on any list, because it never relies on the order of the items.
What is the worst case for linear search on a list of n items?
n comparisons: the target is last or missing.
A target is missing from a list of 200 items. How many comparisons?
200: one for every item.
Why can linear search be slow on a long list?
The number of comparisons in the worst case grows with the length of the list.
What does binary search need that linear search doesn't?
A sorted list.
Long sorted list: which search is usually much faster?
Binary search (usually, not always).
What does each row of a linear search trace record?
One comparison: the position, the item there, the target, and whether it matched.

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 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.

Helpful guides for parents and students