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 = 1262 start at cup 13 compare the number in this cup with the search item4 if they are equal → stop: found it5 if not → move to the next cup, then back to line 36 if there are no cups left → stop: it is not in the list
Variables
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.
Press Next to open the next cup. ▢ is a cup nobody has opened yet.
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
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?
What is the 'search item'?
Where does a linear search begin?
What happens at each position in a linear search?
When does a linear search stop if the item IS in the list?
The search item is missing. How does a linear search find that out?
Does a linear search need a sorted list?
Why reach for a linear search on an unordered list?
Can a linear search look for words (strings)?
What does the performance of an algorithm relate to?
Linear search: best case?
Linear search: worst case?
Worst case for a list of 50 items: how many comparisons?
Give two everyday jobs where a computer searches data.
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 Edexcel GCSE Computer Science topics
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.