GCSE · Computer Science · AQA · Spec 8525

Linear search

Lost a file? The computer has to go looking: check one item, then the next, then the next. That's a linear search.

Computer Science · Algorithms

Trace table — dry run the code

Press Next to run it one comparison at a time. Before each step, say out loud: will this item match the target?

1list ← [12, 5, 9, 7, 3]
2INPUT target
3position ← 1
4WHILE position ≤ LENGTH(list)
5 IF list[position] = target THEN
6 OUTPUT "Found at position", position, then STOP
7 position ← position + 1
8OUTPUT "Not found"
target
position
item
equal?
comparisons
Output
Press Start to run the first line.
·

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

Computer Science · Searching

Best case, worst case, or neither?

Sort each situation: is it the best case, the worst case, or neither? Decide first, then read why.

Still to sort

Best case (0)

The fewest comparisons the search could possibly need.

Where the line is: Best case = the smallest possible number of steps. For a linear search that means the match is the very first item.

Worst case (0)

The greatest number of comparisons: every item gets compared.

Where the line is: Worst case = the greatest number of steps. There are two ways to get it: the item is last, or the item is not in the list at all.

Neither (0)

More than the minimum, but not every item compared.

Where the line is: Found part-way along: more than one comparison, fewer than all of them.

5 of 5 still to sort.

A linear search is hunting through a list of 7 names. Where the name turns up decides how many comparisons it needs.

Computer Science · Searching

Double the list, double the work?

In the worst case, the 5-item list from the trace needs 5 comparisons. What is the largest number of comparisons a linear search could need on a list twice as long, with 10 items?

Your estimate

10 comparisons

0 comparisons20 comparisons

Linear search or binary search?

Linear searchvsBinary search

Which one is better depends on the data being searched and the context. Here we only compare them; binary search gets its own lesson.

Focus

Does the list have to be in order?

Linear search

No. It works on sorted or unsorted lists.

Binary search

Yes. It can only be used when the list is already in order.

The insight

This is the difference people mix up most. If the data isn't in order, binary search is ruled out unless you sort the data first.

How does it move through the list?

Linear search

One item at a time, in sequence.

Binary search

Looks at the middle item of the part still in play, and after every comparison discards the half that can't hold the target.

Which is generally quicker on a sorted list?

Linear search

Generally slower.

Binary search

Generally finds the item more quickly.

Which is simpler to write?

Linear search

Simpler: it steps through the data in sequence.

Binary search

Longer and more complex: the algorithm can follow several different paths.

Computer Science · Searching

Whose search went wrong?

A student ran a linear search for 9 in the list 4, 9, 2, 7. Their working is below. Which line is the first one that goes wrong?

A student's answer — which line goes wrong?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Run a search by hand, count the comparisons, and find out what the best and worst cases really are.

What you need to know

  • Computers search through large amounts of data to find what's needed, such as a file with a particular name.
  • Linear search checks each item in a list one at a time, to see if it's the item you're after.
  • It finds items in any order, so the list can be sorted or unsorted, and it works on strings too.
  • Have a goYour friend says, 'A linear search only works if the list is sorted first.' Are they right?

    No. A linear search works on sorted or unsorted lists.

    It checks the items one at a time, so the order they sit in doesn't matter. Your friend is adding a rule that linear search doesn't have.

  • Start at the first item and compare it with the search item. If it isn't a match, go to the next item.
  • Stop when the current item equals the search item. If all items are checked with no match, it isn't in the list.
  • Have a goSearch for 6 in the list 8, 3, 6, 1. Which items does a linear search look at before it stops?

    8, then 3, then 6. The 1 is never looked at.

    It stops the moment the current item equals the search item, so anything after the match never gets checked.

  • A search algorithm's performance relates to the number of steps (comparisons) it takes to complete.
  • Best case: the search item is the very first one, so the search takes the smallest possible number of steps.
  • Have a goA list holds 1,000 names and the one you want is the very first name. How many comparisons does a linear search make?

    Just one. That's the best case.

    Best case means the smallest possible number of steps, and a match on the first item takes a single comparison. The length of the list doesn't change that.

  • Worst case: the search item is last in the list or not in it at all, so every item is compared.
  • In the worst case, doubling the length of the list doubles the largest number of comparisons it may need.
  • Binary search needs a list already in order, and generally finds an item more quickly on a sorted list.

The big picture

A linear search checks the items in a list one at a time, comparing each with the search item. It stops on a match; if every item has been checked with no match, the item isn't in the list. It works on sorted or unsorted lists. The best case is the search item being first; the worst case is it being last or not in the list, so every item is compared. Binary search needs an ordered list and generally finds items more quickly there, but linear search is simpler to write.

Key points

1Linear search checks the items one at a time, in order, comparing each with the search item.
2It stops on a match. If every item has been checked and none matched, the item isn't in the list.
3It works on sorted or unsorted lists, and on different types of data, including strings.
4Performance relates to the number of steps (comparisons).
5Best case: search item first. Worst case: search item last or not in the list, so every item is compared.
6In the worst case, doubling the list doubles the largest number of comparisons.
7Binary search needs an ordered list and generally finds items more quickly there; linear search is simpler to write. The better choice depends on the data and the context.

Worked example

Problem

The names in a list are: Mo, Eve, Lou, Kai, Jay, Ana. Use a linear search to find Kai and say how many comparisons it takes. Then search for Sam and say how many comparisons that takes.

⚠ Watch out

Stopping at the first item that doesn't match. One mismatch only rules out one item. The search carries on until it finds a match or has compared every item. And the worst case isn't only 'the item is last': an item that isn't in the list is a worst case too.

🧠

Memory hook

Check, check, check: carry on until it matches, or until you've run out of items.

✓

Check yourself

In the list 15, 3, 8, 20, how many comparisons does a linear search need to find 20? And to find 8? Which is the worst case?

Flashcards

(13)
What does a linear search do?
It checks each item in a list one at a time, one after another, to see if it is the item being searched for.
Where does a linear search start, and what does it do at each item?
It starts at the first item and compares the item at the current position with the search item. If it isn't a match, it goes to the next item.
When does a linear search stop?
When the item at the current position equals the search item, or when all the items have been checked.
How do you know an item is not in the list?
Every item has been checked and none of them matched the search item.
Does a linear search need the list to be in order?
No. It can find items in any order, so the list can be sorted or unsorted. It works on different types of data too, including strings.
What does the performance of a search algorithm relate to?
The number of steps (comparisons) it takes to complete.
What is the best case for a linear search?
The search item is the very first one in the list. That is the smallest possible number of steps.
What are the two worst cases for a linear search?
The search item is the very last one in the list, or it is not in the list at all. Either way every item is compared.
A list is twice as long. What happens to the largest number of comparisons a linear search may need?
It doubles, because in the worst case every item is compared.
When can a binary search be used?
Only when the list is already in order.
How does a binary search work?
It looks at the middle item of the part of the list still in play. After every comparison it discards the half that cannot hold the target, until the target is found or nothing is left.
Which is simpler to write, linear search or binary search?
Linear search. It steps through the data in sequence. Binary search is longer and more complex because it can follow several different paths.
Which search is the better choice, linear or binary?
It depends on the data being searched and the context.

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 AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson 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 4 November 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.