Cambridge IGCSE · Topic 7 (3-4)
IGCSE Computer Science 0478 / Topic 7 / IGCSE

Find it, then sort it.

Two standard methods appear again and again in exam algorithms. A linear search checks items one by one until it finds what it wants. A bubble sort swaps neighbouring items until the whole list is in order. Learn both well enough to write them from memory.

The key idea

A linear search checks each item of a list in turn, from the first, until the item is found or the end of the list is reached. A bubble sort compares each pair of adjacent items and swaps them if they are in the wrong order. One pass moves the largest remaining item to the end. Passes repeat until a whole pass makes no swaps, so the list is sorted.

Bubble sort: the first pass through 7, 3, 9, 1, 5 Start 7 3 9 1 5 compare 1 and 2: swap 3 7 9 1 5 compare 2 and 3: no swap 3 7 9 1 5 compare 3 and 4: swap 3 7 1 9 5 compare 4 and 5: swap 3 7 1 5 9 in place
Fig. 1: in the first pass the largest value, 9, bubbles along to the end of the list
Section 01

Linear search.

Step through the search in the lab. Each step compares one item with the target; the search stops as soon as it is found, or when every item has been checked.

DECLARE Names : ARRAY[1:6] OF STRING
DECLARE Index : INTEGER
DECLARE Found : BOOLEAN
DECLARE SearchName : STRING
// Names[1] to Names[6] hold: Ali, Bea, Chen, Dev, Eva, Finn
OUTPUT "Enter the name to find"
INPUT SearchName
Found ← FALSE
Index ← 1
WHILE Found = FALSE AND Index <= 6 DO
    IF Names[Index] = SearchName
      THEN
        Found ← TRUE
      ELSE
        Index ← Index + 1
    ENDIF
ENDWHILE
IF Found = TRUE
  THEN
    OUTPUT SearchName, " found at position ", Index
  ELSE
    OUTPUT SearchName, " not found"
ENDIF
Worked example Search for Dev
Using the algorithm above, how many names are compared when the user enters Dev, and what is output?
  1. Index 1: Ali is not Dev, so Index becomes 2.
  2. Index 2: Bea is not Dev; Index 3: Chen is not Dev; Index becomes 4.
  3. Index 4: Dev matches, so Found becomes TRUE and the loop stops.
  4. 4 comparisons were made, and Index is still 4.
Answer: 4 comparisons; output "Dev found at position 4"
Your turn
How many comparisons are made if the user enters Gus, and what is output?
Examiner trap

Do not stop the search with only one condition. The loop needs both: stop when the item is found or when the end of the list is reached, otherwise a missing item makes Index run past the end of the array.

Section 02

Bubble sort.

Watch the bubble sort in the lab. Two neighbours are compared; if the left one is bigger they swap. At the end of each pass the largest unsorted value has reached its final place.

DECLARE Values : ARRAY[1:5] OF INTEGER
DECLARE Index, Last, Temp : INTEGER
DECLARE Swap : BOOLEAN
// Values holds 7, 3, 9, 1, 5
Last ← 5
REPEAT
    Swap ← FALSE
    FOR Index ← 1 TO Last - 1
        IF Values[Index] > Values[Index + 1]
          THEN
            Temp ← Values[Index]
            Values[Index] ← Values[Index + 1]
            Values[Index + 1] ← Temp
            Swap ← TRUE
        ENDIF
    NEXT Index
    Last ← Last - 1
UNTIL Swap = FALSE OR Last = 1
Examiner trap

Swapping with Values[Index] ← Values[Index + 1] followed by Values[Index + 1] ← Values[Index] does not swap: both end up as the same value. Always use a temporary variable.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

How does a linear search look for an item?

Quick check+10 XP

In a bubble sort, which items are compared?

Quick check+10 XP

Why does the bubble sort use the variable Temp?

Quick check+10 XP

In the lab, after the first pass of a bubble sort into ascending order, where is the largest value?

Section 03

Pass by pass.

Exam questions often ask for the list after each pass. This table follows the algorithm above on 7, 3, 9, 1, 5. The pseudocode lab's standard methods tab runs the same kind of sort on its own data.

PassComparisonsSwapsList after the pass
Start7, 3, 9, 1, 5
1433, 7, 1, 5, 9
2323, 1, 5, 7, 9
3211, 3, 5, 7, 9
4101, 3, 5, 7, 9 (no swaps: stop)
Your turn
Write the list 6, 2, 8, 4 after the first pass of a bubble sort into ascending order.
Examiner trap

The list can be sorted before the algorithm knows it. After pass 3 it is in order, but one more pass is needed to make no swaps and confirm it.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

A list has 40 items. What is the greatest number of comparisons a linear search can make?

Exam style+20 XP

What is the list 5, 1, 4, 2 after ONE pass of a bubble sort into ascending order?

Exam style+20 XP

How does a bubble sort with a swap flag know that the list is sorted?

Exam style+20 XP

Which condition correctly ends a linear search loop?

Skill unlocked

Linear search and bubble sort, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Writing and tracing a linear search and a bubble sort, and showing a list after each pass.