← IGCSE Computer Science Paper 2

Linear search: how it works

Checks each item in a list in turn, from the start, until the target is found or the end of the list is reached. It works on unsorted data.

Understanding Linear search: how it works in IGCSE Computer Science Paper 2

What it does: Index 1 is the first item. If Names[1] equals the target, the search can finish after just one comparison. The loop runs through all 10 items. When the target is last or missing, a linear search makes all 10 checks. The search checks 4, then 9, then 7. It stops at 7, which is the third item, so 3 checks are made. On average the target is somewhere near the middle, so about half of the items (around 50) are checked. Checks each item in a list in turn, from the start, until the target is found or the end of the list is reached. It works on unsorted data.

Common mistakes: The flag must start as FALSE because the target has not been found yet. If it starts as TRUE, the search would report found even when the target is not in the list. It should be Found ← FALSE. The list has 10 items, so the loop must check every one, from 1 to 10. Stopping at 9 means the last item is never checked. It should be FOR I ← 1 TO 10.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 286–295) and word search (Modules 275–295).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A linear search checks the items of a list in which order?
Starting from the middle of the listOne at a time, from the start of the list
Matches
Linear search needs the list to be sorted into order first, otherwise it cannot work
↓
A linear search works on unsorted data. It simply checks each item in turn, so no sorting is needed.
Applying
Meanings
Term
linear search
Meaning
Checks each item in a list in turn, from the start, until the target is found or the end of the list is reached. It works on unsorted data.
Practice
Found ← FALSE
Testing
Gaps
Found ←
Check Answer
Spots
1Found ← TRUE
2FOR I ← 1 TO 10

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Polar Bear' badge — Data Heavyweight, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A linear search checks the items of a list in which order?
A. One at a time, from the start of the list ✓
B. Starting from the middle of the list
A linear search starts at the first item and checks each item in turn. Starting in the middle is a different method (binary search).
2. Can a linear search be used on a list that is NOT sorted?
A. No, the list must be sorted first
B. Yes, it works on unsorted data ✓
A linear search checks every item in turn, so the order of the data does not matter. It works on unsorted data.
3. A linear search is looking for the target 7 in the list [4, 9, 7, 2]. How many items are checked before 7 is found?
A. 3 ✓
B. 4
The search checks 4, then 9, then 7. It stops at 7, which is the third item, so 3 checks are made.
4. A linear search looks for the target 5 in [8, 3, 6, 1]. What is the result?
A. Not found, after checking all 4 items ✓
B. Not found, after checking only the first item
5 is not in the list. The search can only say 'not found' after it has checked every item to the end of the list.
5. What is the BEST case for a linear search?
A. The target is the last item, so only 1 check is needed
B. The target is the first item, so only 1 check is needed ✓
The best case is when the target is the first item in the list. The last item is the worst case, because every item must be checked.

Match the Pairs

FOR Index ← 1 TO 10 IF Names[Index] = Target THEN Found ← TRUE → Checks each item in the list in turn, starting from the first, to see if it equals the target
Found ← FALSE → Starts the search with the target not yet found; it only becomes TRUE if a matching item turns up
Best case: the target is the very first item, so only 1 check is needed → Names[1] = Target
Worst case: the target is the last item or not in the list, so every item is checked → FOR Index ← 1 TO 10
Linear search needs the list to be sorted into order first, otherwise it cannot work → A linear search works on unsorted data. It simply checks each item in turn, so no sorting is needed.
On average, a linear search of 100 items needs 1 check to find the target → The average case is about half the list, around 50 checks. Only the best case needs 1 check.

Key Term

linear search
Checks each item in a list in turn, from the start, until the target is found or the end of the list is reached. It works on unsorted data.

Fill in the Blanks

1. Target ← 7 Index ← 0 Found ← FALSE
Hint: Type the Boolean value the flag starts with, because we have not found the target yet.
2. // Scores has 5 items (index 0 to 4) FOR Index ← 0 TO 4
Hint: Type the index of the last item, so every item is checked in turn.
3. FOR Index ← 0 TO 4 IF Scores[Index] = Target THEN Found ← TRUE
Hint: Type the comparison operator that checks whether the current item matches the target.
4. IF Scores[Index] = Target THEN Found ← TRUE ENDIF
Hint: Type the Boolean value the flag is set to when the target is found.
5. // Worst case: target is last or missing // List has 5 items, so most checks is: Checks ← 5
Hint: Type how many items must be checked in the worst case, which is the whole list.

Spot the Error

Question 1
A Found ← TRUE
B FOR I ← 1 TO 10
C IF List[I] = Target THEN
D Found ← TRUE
E ENDIF
F NEXT I
Line A — The flag must start as FALSE because the target has not been found yet. If it starts as TRUE, the search would report found even when the target is not in the list. It should be Found ← FALSE.
Question 2
A Found ← FALSE
B FOR I ← 1 TO 9
C IF List[I] = Target THEN
D Found ← TRUE
E ENDIF
F NEXT I
Line B — The list has 10 items, so the loop must check every one, from 1 to 10. Stopping at 9 means the last item is never checked. It should be FOR I ← 1 TO 10.
Question 3
A Found ← FALSE
B FOR I ← 1 TO 10
C IF List[I] ← Target THEN
D Found ← TRUE
E ENDIF
F NEXT I
Line C — A search compares each item with the target, so it needs the comparison operator =. The arrow ← is for assignment. It should be IF List[I] = Target THEN.
Question 4
A Found ← FALSE
B FOR I ← 1 TO 10
C IF List[I] = Target THEN
D Found ← FALSE
E ENDIF
F NEXT I
Line D — When the current item matches the target, the flag must be set to TRUE to record that the target was found. Setting it to FALSE means the match is never recorded. It should be Found ← TRUE.
Question 5
A Position ← 0
B FOR I ← 1 TO 10
C IF List[I] = Target THEN
D Position ← Target
E ENDIF
F NEXT I
Line D — The position of a match is the index of the item, which is the loop counter I. Storing Target just copies the value being searched for. It should be Position ← I.

Student with a class code? Join your class →