REVISION GUIDE · IGCSE 0478 AND A LEVEL 9618
Linear search and bubble sort in pseudocode
Exam-ready Cambridge pseudocode for a linear search and a bubble sort, a pass-by-pass trace, and the slips that stop the code from working.
1. Where these appear in the exam
Linear search and bubble sort are two of the standard methods of solution in IGCSE 0478 Paper 2, alongside totalling, counting, and finding the maximum, minimum and average. You may be asked to write them, complete them, find an error in them or trace them. A Level 9618 builds on them with binary search and insertion sort.
2. Linear search
A linear search checks each element in turn, starting at the first, until it finds the target or reaches the end of the array. It works on unsorted data.
DECLARE Names : ARRAY[1:10] OF STRING
DECLARE Index : INTEGER
DECLARE Found : BOOLEAN
DECLARE Target : STRING
Found ← FALSE
Index ← 1
INPUT Target
WHILE Found = FALSE AND Index <= 10 DO
IF Names[Index] = Target
THEN
Found ← TRUE
ELSE
Index ← Index + 1
ENDIF
ENDWHILE
IF Found = TRUE
THEN
OUTPUT "Found at position ", Index
ELSE
OUTPUT "Not found"
ENDIF3. Bubble sort
A bubble sort compares each pair of neighbouring elements and swaps them if they are in the wrong order. After each pass the largest remaining value has “bubbled” to the end, so the next pass can stop one place earlier.
Last ← 10
REPEAT
Swapped ← FALSE
FOR Index ← 1 TO Last - 1
IF Numbers[Index] > Numbers[Index + 1]
THEN
Temp ← Numbers[Index]
Numbers[Index] ← Numbers[Index + 1]
Numbers[Index + 1] ← Temp
Swapped ← TRUE
ENDIF
NEXT Index
Last ← Last - 1
UNTIL Swapped = FALSE OR Last = 1The Swapped flag ends the sort early: if a whole pass makes no swaps, the array is already in order.
4. Trace: sorting 5, 1, 4, 2
| Pass | Comparisons and swaps | Array after the pass |
|---|---|---|
| 1 | 5↔1 swap, 5↔4 swap, 5↔2 swap | 1, 4, 2, 5 |
| 2 | 1 and 4 no swap, 4↔2 swap | 1, 2, 4, 5 |
| 3 | 1 and 2 no swap, so Swapped stays FALSE | 1, 2, 4, 5 (sort stops) |
Notice that each pass makes one fewer comparison because Last goes down by 1.
5. Mistakes that cost marks
- Swapping without a temporary variable, so one value is overwritten and lost.
- Looping
FOR Index ← 1 TO Lastin a bubble sort, soNumbers[Index + 1]goes past the end of the array. - Forgetting to reset
Swapped ← FALSEat the start of each pass, so the loop never ends. - Outputting “Not found” inside the search loop, so it prints many times.
- Forgetting to increase
Indexin a WHILE loop, which creates an infinite loop.
6. How to practise
Write the code out by hand from memory, then trace it with a short list. Our pseudocode guide covers the Cambridge conventions. The IGCSE Algorithm design page has the full video lesson on algorithms and flowcharts, and our trace table guide shows how to set out a trace.
Frequently asked questions
Do I need binary search for IGCSE Computer Science?
No. IGCSE 0478 asks for linear search and bubble sort. Binary search and insertion sort are on the A Level 9618 syllabus.
Does a linear search need the data to be sorted?
No. A linear search checks every element in turn, so it works on sorted or unsorted data. A binary search does need sorted data.
Why does a bubble sort use a temporary variable?
To swap two values safely. If you copy one value straight over the other, the first value is lost, so it is stored in Temp first.
Want help with Paper 2 algorithms? In a 1-to-1 lesson we can write, trace and debug search and sort code together. Book a free demo →