GCSE · Computer Science · Edexcel · Spec 1CP2

Linear search

Nine cups. One hides 126. You can lift one cup at a time — where do you start, and how many might you lift?

Open the cups

Find 126, one cup at a time

Nine cups sit in a row, in no particular order, and each one hides a number. Step through the search and watch which cups get opened — and which ones never do.

Before each click, guess: is this the cup? And how many comparisons have we made so far?

1 search item = 126
2 start at cup 1
3 compare the number in this cup with the search item
4 if they are equal → stop: found it
5 if not → move to the next cup, then back to line 3
6 if there are no cups left → stop: it is not in the list

Variables

cups=▢ ▢ ▢ ▢ ▢ ▢ ▢ ▢ ▢comparisons=0search item=126

Output

 

Step 1: Nine closed cups in a random order. We're hunting for 126, and we can't see inside a cup until we open it.

1 / 9

Press Next to open the next cup. ▢ is a cup nobody has opened yet.

Step 1 of 9: Nine closed cups in a random order. We're hunting for 126, and we can't see inside a cup until we open it..

Watch out: The search never peeks ahead. It only knows what's in the cups it has already opened.

Have a go

Your turn: search a list of words

The list is Moscow, Sydney, Beijing, Athens, Mumbai, Tokyo, Prague, and the search item is Mumbai. The search has started — you pick each missing step.

  1. The search item is Mumbai. Start at the first item in the list, Moscow.
  2. Compare Moscow with Mumbai. Not equal, so move to the next item, Sydney. (1 comparison)
  3. missing step
Which line is step 3?

Exam line: Count every comparison, including the one that finds the match.

What do you reckon?

Will a linear search work here?

Sam's music app keeps 200 song titles in the order they were added, not in alphabetical order. Sam wants to use a linear search to find a song called 'Neon Rain'.

Which is closest to what you think right now?
How sure are you?

How long does it take?

Best case and worst case

The list is Dublin, Cairo, La Paz, Seoul, New York, London, Paris. Drag each search item to the number of comparisons a linear search makes before it stops. (Berlin isn't in the list — where do you think it lands?)

Write it, then mark it

Put it in your own words

A club stores the names of its 30 members in a list, in the order they joined — not in alphabetical order. (a) Describe how a linear search would find one member's name in this list. (b) Explain why a linear search is a suitable choice for this list. (c) State the best case and the worst case for this search. [5 marks]

0 words · your answer stays on this page and is not sent anywhere.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Check every item, one after another, until you find what you're after — or run out of list.

What you need to know

  • A linear search hunts for one item — the search item — by checking a list one item after another.
  • Computers do this constantly: finding a file with a particular name, or websites that match your keywords.
  • It always kicks off at the very first item in the list. No skipping ahead.
  • At each position, compare the current item with the search item. Equal? Stop. Not equal? Go to the next item.
  • Have a goJordan searches bus, train, bike, car for 'bike'. After bus doesn't match, Jordan leaps straight to car 'because the end feels lucky'. Which item should Jordan have compared next?

    Train — the very next item.

    After a miss, a linear search always moves to the next item along. Luck doesn't come into it, and leaping ahead means train never gets checked.

  • 'Not in the list' is only allowed once every single item has been checked.
  • Have a goA search has compared 9 items out of 10 and found no match. Is it allowed to say 'not in the list' yet?

    No — one item still hasn't been compared.

    Nine misses feel convincing, but the search item could be sitting in that last unchecked spot. Every item has to be compared first.

  • Order doesn't matter: sorted or unsorted, it still works. If the list isn't in order, it's the only reasonable way to search.
  • Numbers, words, anything: a linear search works on a list of any type of data, including strings.
  • Have a goList: Moscow, Sydney, Beijing, Athens, Mumbai, Tokyo, Prague. A linear search looks for Athens. How many comparisons before it stops?

    4

    Moscow, Sydney, Beijing, then Athens. It's tempting to say 3, but the comparison that finds Athens counts too.

  • An algorithm's performance is about how many steps it takes to finish. Here, count the comparisons.
  • Best case: the search item is first, so just 1 comparison.
  • Worst case: it's last, or not there at all, so the whole list gets checked.

The big picture

A linear search is the simplest way to hunt for something in a list: start at the first item, compare it with the search item, and keep moving to the next one until they're equal. The moment it finds a match, it stops. It can only say 'not in the list' after every item has been checked. Order doesn't matter and neither does the type of data, so for a list that isn't in order it's the only reasonable way to search. Best case: the first item, 1 comparison. Worst case: the last item or one that isn't there, so the whole list gets checked.

Key points

1A linear search looks for a search item by examining each item in a list, one after another, starting from the first.
2At each position, compare the current item with the search item: if they're equal, stop; if not, move to the next item.
3The search can only report that the item is not in the list after every item has been checked.
4It works on lists in any order, sorted or unsorted, and on any type of data, including strings. For an unordered list, it's the only reasonable way to search.
5The performance of an algorithm relates to the number of steps it takes to complete. For a linear search, count the comparisons.
6Best case: the search item is the first item, so 1 comparison. Worst case: it's the last item or isn't in the list, so every item is compared.

Worked example

Problem

A list holds eight playing cards in this order: four, ten, five, two, eight, seven, nine and three of spades. Use a linear search to look for the six of spades. How many comparisons are made, and what is the result?

⚠ Watch out

Giving up early. When the item hasn't turned up yet, it's really tempting to decide it isn't there — but the list can be in any order, so it could be the very last one. 'Not in the list' is only true once every item has been compared.

🧠

Memory hook

Lift, look, move on. Stop the moment you find it — but you can't say 'it's not here' until you've lifted the very last cup.

✓

Check yourself

List: 15, 8, 42, 3, 27. How many comparisons to find 3? And to search for 50? (4 — 3 is the 4th item. 5 — 50 isn't there, so every item is checked.)

Flashcards

(14)
What is a linear search?
An algorithm that searches for an item in a list by examining each item, one after another, to see if it's the item being searched for.
What is the 'search item'?
The item you're hunting for. Every item in the list gets compared with it.
Where does a linear search begin?
At the start of the list, position one — never in the middle, never at the end.
What happens at each position in a linear search?
Compare the item at the current position with the search item. Not equal? Go to the next item.
When does a linear search stop if the item IS in the list?
The moment the item at the current position is equal to the search item. Nothing after it gets looked at.
The search item is missing. How does a linear search find that out?
It compares every single item, right to the end. Only when none of them matched can it say 'not in the list'.
Does a linear search need a sorted list?
No. It finds items in any order, sorted or unsorted.
Why reach for a linear search on an unordered list?
If a list isn't in order, a linear search is the only reasonable way to search through it.
Can a linear search look for words (strings)?
Yes. It works on lists of any type of data, including strings — not just numbers.
What does the performance of an algorithm relate to?
The number of steps it takes to complete. For a linear search, count the comparisons.
Linear search: best case?
The search item is the very first item in the list, so just 1 comparison.
Linear search: worst case?
The search item is the last item, or isn't in the list at all — either way the whole list is checked.
Worst case for a list of 50 items: how many comparisons?
50 — every item gets compared.
Give two everyday jobs where a computer searches data.
Finding a file with a particular name, and finding websites that match some keywords.

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

See the full Edexcel Computer Science curriculum →

How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)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 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.