GCSE · Computer Science · OCR · Spec J277
Linear search
Computers need to hunt through lists, like finding a file with the right name. Here's the most patient way to do it, and when it gets lucky.
Computer Science · Searching
Linear search, one comparison at a time
A linear search looks at each item in turn, starting from the first, until it finds the one it wants or runs out of list. Step through two searches of one list: a city that is there, then one that isn't.
Search 1 looks for Athens. Search 2 looks for Berlin. Where does each one stop?
1 cities = ["Moscow", "Sydney", "Beijing", "Athens", "Mumbai", "Tokyo", "Prague"]2 searchItem = input("City to find?")3 position = 04 found = False5 while found == False AND position < cities.length6 if cities[position] == searchItem then7 found = True8 else9 position = position + 110 endif11 endwhile12 if found == True then13 print("Found it")14 else15 print("Not in the list")16 endif
Variables
Output
Step 1: Here is the list. It's in no particular order. A linear search never asks for one.
Press Next and watch position and found change. The page shows the steps; it doesn't run the program for you.
Predict, then check
Think about every item you could be asked to find, including one that isn't there.
A list has 7 items. What are the fewest and the most comparisons a linear search could need?
Key terms
Say the definition out loud, then flip the card.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
How a computer hunts through a list, and why the position of what it wants decides how long it takes.
What you need to know
- Computers need to search through lists of data, for example to find a file with a particular name or websites matching keywords.
- Linear search is an algorithm: it examines each item in the list, one after another, looking for the search item.
- To start, take the list and the item you want (the search item), then begin at the first item.
- Compare the current item with the search item: if it's different, go to the next item; if it's equal, stop searching.
Have a goQuick one: you're hunting for 'Sam' in the list Mia, Sam, Leo. How many comparisons before the search stops?
Two: Mia, then Sam.
Mia is different, so you go to the next item. Sam is equal, so you stop, and Leo never gets looked at.
- If you've worked through every item and never found a match, the search item is not in the list.
- Sorted or unsorted, it doesn't matter. If a list isn't in order, linear search is the only reasonable way to search it.
- It isn't just for numbers. A linear search works on lists of many types of data, including strings.
- Performance is the number of steps an algorithm takes; depending on the data, it can be a best case or a worst case.
- Best case for linear search: the search item is the very first one, so it takes just one comparison.
Have a goYour friend's list has a million items, and the one they want happens to be first. Do they need a million comparisons?
No. Just one comparison.
Best case depends on where the search item sits, not how long the list is. It's first, so the very first comparison matches and the search stops.
- At the other extreme, the item is the very last one or isn't there at all: every item gets compared, so seven comparisons in a list of seven.
Have a goBad-luck contest: the item you want is the very last one in the list, or it isn't in the list at all. Which is worse luck?
It's a tie.
In both cases every item gets compared before the search ends, so both are the worst case.
The big picture
A linear search examines each item in a list, one after another, from the first: if the current item is different from the search item it moves on, if it is equal it stops, and if every item has been compared with no match, the item is not in the list. It works on sorted or unsorted lists and on many types of data, including strings. Its performance is the number of steps: best case, the item is first (one comparison); worst case, it is last or not in the list (every item compared).
Key points
Worked example
Problem
Use a linear search to find 'song.mp3' in this list of file names: report.docx, photo.jpg, notes.txt, song.mp3, budget.xlsx. Then say what happens if you search for 'video.mp4' in the same list instead.
⚠ Watch out
Saying 'not in the list' as soon as one item fails to match. A mismatch only means 'go to the next item'. You can only say the search item isn't there once every item has been compared and none matched.
Memory hook
Think of checking names down a register: tick each one off until you hit the name you want, or run out of names. Where the name sits decides your luck: first on the list is one check, last on the list (or not there) is a check for every name.
Check yourself
Cover the page and write out the steps of a linear search, including how it ends. Then say where the search item sits in the best case, and in the worst.
Flashcards
(12)Why do computers need to be able to search through data?
What is a linear search?
What is the 'search item'?
Where does a linear search start?
The current item isn't the search item. What does a linear search do?
What makes a linear search stop searching?
How does a linear search conclude the search item is not in the list?
Does a linear search need the list to be sorted?
Can a linear search be used on strings, or only numbers?
What does the performance of an algorithm relate to?
What is the best case for a linear search?
What is the worst case for a linear search?
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 OCR GCSE Computer Science topics
How this lesson was checked. This OCR GCSE Computer Science (specification J277)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 13 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.