Exam technique · 8 min read

IGCSE Computer Science Bubble Sort & Linear Search (0478)

IGCSE Computer Science bubble sort and linear search (0478) explained: pseudocode, trace tables, worked examples and the mistakes that cost marks.

Bubble sort and linear search are the two searching and sorting methods named in the Cambridge IGCSE Computer Science (0478) syllabus. They turn up in Paper 2, where you may be asked to describe them, complete a trace table, find an error or write the algorithm yourself. This guide gives you a method for each, with worked pseudocode you can trace by hand.

Binary search isn't on the list, so you don't need it for the exam.

What the 0478 syllabus actually says

Topic 7 of the 2026–2028 syllabus includes the learning outcome "Understand standard methods of solution". The notes limit this to linear search, bubble sort, totalling, counting, and finding maximum, minimum and average values (Cambridge International, 0478 syllabus 2026–2028).

Paper 2 (Algorithms, Programming and Logic) lasts 1 hour 45 minutes, carries 75 marks and is half of the qualification. On the same paper you may be asked to complete a trace table, identify errors in an algorithm and write algorithms in pseudocode or flowcharts.

Tip: Programming code earns no marks on most of Paper 2. The syllabus makes an exception for the 15-mark scenario question. Learn the Cambridge pseudocode style for everything else.

Linear search: how it works

A linear search checks each item in a list one at a time, starting at the first, until it finds the target or reaches the end.

  1. 1Start at the first item.
  2. 2Compare it with the value you are looking for.
  3. 3If they match, stop and report the position.
  4. 4If not, move to the next item.
  5. 5If you pass the last item without a match, report "not found".

The list does not have to be sorted. That is the main strength of a linear search. The weakness is speed: in the worst case you check every item.

Worked example: linear search in pseudocode

This algorithm searches five names for the name the user types in.

DECLARE Names : ARRAY[1:5] OF STRING
DECLARE SearchName : STRING
DECLARE Index : INTEGER
DECLARE Found : BOOLEAN

Names[1] ← "Asha"
Names[2] ← "Ben"
Names[3] ← "Chen"
Names[4] ← "Dina"
Names[5] ← "Eli"

Found ← FALSE
Index ← 1
INPUT SearchName
WHILE Index <= 5 AND Found = FALSE DO
  IF Names[Index] = SearchName
    THEN
      Found ← TRUE
    ELSE
      Index ← Index + 1
  ENDIF
ENDWHILE
IF Found = TRUE
  THEN
    OUTPUT "Found at position ", Index
  ELSE
    OUTPUT "Not found"
ENDIF

Now trace it when the user enters Dina:

IndexNames[Index]FoundOUTPUT
1AshaFALSE
2BenFALSE
3ChenFALSE
4DinaTRUE
4TRUEFound at position 4

Index stops increasing at 4 because the ELSE branch does not run once the match is found. If you entered Zara, Index would reach 6, the loop condition would fail and the output would be Not found.

Tip: In a trace table, write a value only when it changes. Recheck the loop condition every time you return to the top of the loop. Our trace tables guide has a five-step method for this.

Bubble sort: how it works

A bubble sort puts a list in order by comparing neighbouring items and swapping them if they are in the wrong order. After the first pass, the largest value has moved ("bubbled") to the end. After each further pass, the next largest settles into place.

Here is the method for sorting into ascending order:

  1. 1Compare the first two items.
  2. 2If the first is bigger than the second, swap them.
  3. 3Move along one place and repeat until you reach the end of the list. This is one pass.
  4. 4Repeat passes until a whole pass makes no swaps (or until you have done enough passes for the length of the list).

Swapping needs a temporary variable. If you write A ← B and then B ← A, both end up holding the same value.

Worked example: bubble sort in pseudocode

Sort five scores: 5, 3, 8, 1, 4.

DECLARE Scores : ARRAY[1:5] OF INTEGER
DECLARE Outer : INTEGER
DECLARE Inner : INTEGER
DECLARE Temp : INTEGER

FOR Outer ← 1 TO 4
  FOR Inner ← 1 TO 4
    IF Scores[Inner] > Scores[Inner + 1]
      THEN
        Temp ← Scores[Inner]
        Scores[Inner] ← Scores[Inner + 1]
        Scores[Inner + 1] ← Temp
    ENDIF
  NEXT Inner
NEXT Outer

Here is the list after each pass:

PassList after the passSwaps in the pass
Start5, 3, 8, 1, 4
13, 5, 1, 4, 83
23, 1, 4, 5, 82
31, 3, 4, 5, 81
41, 3, 4, 5, 80

Look at pass 1 closely, comparing neighbouring pairs:

  • 5 and 3: swap, giving 3, 5, 8, 1, 4
  • 5 and 8: no swap
  • 8 and 1: swap, giving 3, 5, 1, 8, 4
  • 8 and 4: swap, giving 3, 5, 1, 4, 8

A list of five items needs at most four passes, because each pass fixes one value in its final place.

A common improvement: stop early

Pass 4 above changed nothing, which tells you the list is already sorted. Many textbooks add a Swapped flag (a Boolean) that is set to FALSE at the start of each pass and set to TRUE whenever a swap happens. If it is still FALSE at the end of a pass, the algorithm stops. Both versions are valid bubble sorts, so use whichever your teacher has taught you, and write it consistently.

Tip: To practise, take any list of five numbers, predict how many swaps it will need, then trace it. The number of swaps equals the number of pairs that are the wrong way round, so 5, 3, 8, 1, 4 needs six.

The other standard methods: totalling, counting, max, min, average

The syllabus pairs searching and sorting with five simpler routines. All of them use a loop and a starting value.

DECLARE Data : ARRAY[1:5] OF INTEGER
DECLARE Total : INTEGER
DECLARE Count : INTEGER
DECLARE Max : INTEGER
DECLARE Min : INTEGER
DECLARE Average : REAL
DECLARE Index : INTEGER

Total ← 0
Count ← 0
Max ← Data[1]
Min ← Data[1]
FOR Index ← 1 TO 5
  Total ← Total + Data[Index]
  IF Data[Index] > 10
    THEN
      Count ← Count + 1
  ENDIF
  IF Data[Index] > Max
    THEN
      Max ← Data[Index]
  ENDIF
  IF Data[Index] < Min
    THEN
      Min ← Data[Index]
  ENDIF
NEXT Index
Average ← Total / 5

If Data holds 12, 7, 19, 4 and 8, the algorithm ends with Total = 50, Count = 2 (the values above 10 are 12 and 19), Max = 19, Min = 4 and Average = 10.

Two points matter here:

  • Start Total and Count at 0. Without this, the answer starts from whatever was in the variable before.
  • Start Max and Min at the first item. Starting Max at 0 breaks if every value is negative, and starting Min at 0 breaks if every value is positive.

Mistakes that cost marks

  • Swapping without a temporary variable. Both items end up with the same value.
  • Comparing with the wrong item. In bubble sort you compare Scores[Inner] with Scores[Inner + 1], not with Scores[Outer].
  • Going out of range. If the inner loop runs to 5 and the code reads Scores[Inner + 1], it tries to read Scores[6], which does not exist. Stop at one less than the array length.
  • Wrong sort direction. > sorts into ascending order. Use < for descending.
  • Forgetting the "not found" case. A linear search must say what happens when the loop ends without a match.
  • Not initialising variables. Set Found, Total, Count, Max and Min before the loop.
  • Describing instead of applying. If the command word is "complete" or "trace", show values, not a paragraph. See our command words guide for what each word asks for.

How to revise this topic so it sticks

Reading a worked solution feels easy, but it doesn't prove you can produce one. Cognitive psychologists have found that trying to recall information from memory leads to better long-term retention than re-reading it (Roediger & Karpicke, 2006). Here is a routine that builds on that finding:

  1. 1Cover the pseudocode above and write each algorithm from memory.
  2. 2Compare it line by line and mark any gaps.
  3. 3Change the data (a different list, a different search value) and trace it again.
  4. 4Explain each line aloud as if teaching a friend.

Step 4 is why we built TeachVerse around teaching. Teaching a topic to someone else forces you to find the parts you can't yet explain. In TeachVerse you explain a topic to an AI pupil in your own words, and its answers to exam-style questions show where your explanation was thin. For logic-based topics, our logic gates and truth tables guide is a good next step.

Quick checklist

  • Linear search: checks items one by one, works on unsorted lists, can be slow on long lists.
  • Bubble sort: compares neighbours, swaps if out of order, repeats passes until sorted.
  • A swap needs a temporary variable.
  • Initialise totals, counts, flags, maximum and minimum before the loop.
  • Use > for ascending order and < for descending order.
  • In a trace table, change a value only when the code changes it.

TeachVerse is independent and isn't endorsed by Cambridge or Pearson. Syllabus codes only show which course a topic follows.

Ready to test whether you can teach this topic? Try TeachVerse free. The free plan includes 3 new lessons a day and no card is needed.

Sources

Learn it by teaching it.

Free for IGCSE: an AI pupil, topical papers, flashcards and a study plan.

Start free

Keep reading