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 = 0
4 found = False
5 while found == False AND position < cities.length
6 if cities[position] == searchItem then
7 found = True
8 else
9 position = position + 1
10 endif
11 endwhile
12 if found == True then
13 print("Found it")
14 else
15 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.

1 / 31

Press Next and watch position and found change. The page shows the steps; it doesn't run the program for you.

Step 1 of 31: Here is the list. It's in no particular order. A linear search never asks for one..

Check the working

Where does this search go wrong?

A student used a linear search to find Omar in this list: Zara, Ben, Priya, Omar, Chloe. Which line of their working is wrong?

A student's working — which line goes wrong?

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?

What do you think?

What does a linear search need?

A student has a long list of names. They're in no order at all, and the student wants to find one of them with a linear search.

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

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

1A linear search examines each item in a list, one after another, starting at the first, until it finds the search item or has compared every item.
2If the current item is different from the search item, go to the next; if it's equal, stop; if every item has been compared with no match, the item is not in the list.
3It works on sorted or unsorted lists and on many types of data, including strings; for a list that isn't in order it's the only reasonable way to search.
4Performance is the number of steps taken. Best case: the search item is first (one comparison). Worst case: it's last or not in the list, so every item is compared.

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?
To search through sequences of data, for example to find a file with a particular name, or websites that match certain keywords.
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 is the item being searched for.
What is the 'search item'?
The item you are looking for in the list.
Where does a linear search start?
At the first item in the list.
The current item isn't the search item. What does a linear search do?
It goes to the next item in the list.
What makes a linear search stop searching?
The item at the current position is equal to the search item.
How does a linear search conclude the search item is not in the list?
The steps have been completed for every item and the search item has not been found.
Does a linear search need the list to be sorted?
No. It finds items in any order, so the list can be sorted or unsorted. For a list not in order, it's the only reasonable way to search it.
Can a linear search be used on strings, or only numbers?
Not just numbers: it works on lists of many different types of data, including strings.
What does the performance of an algorithm relate to?
The number of steps it takes to complete. Depending on the input data, that can be a best-case or a worst-case scenario.
What is the best case for a linear search?
The search item is the very first one in the list: one comparison.
What is the worst case for a linear search?
The item is the very last one, or isn't in the list at all, so every item is compared (seven comparisons in a list of seven).

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

See the full OCR Computer Science curriculum →

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.