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?
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.
A linear search is hunting through a list of 7 names. Where the name turns up decides how many comparisons it needs.
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
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?
Where does a linear search start, and what does it do at each item?
When does a linear search stop?
How do you know an item is not in the list?
Does a linear search need the list to be in order?
What does the performance of a search algorithm relate to?
What is the best case for a linear search?
What are the two worst cases for a linear search?
A list is twice as long. What happens to the largest number of comparisons a linear search may need?
When can a binary search be used?
How does a binary search work?
Which is simpler to write, linear search or binary search?
Which search is the better choice, linear or binary?
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 AQA GCSE Computer Science topics
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.