🎯 Syllabus & Goals 3 min
Cambridge 7.4 · Standard methods — linear search, bubble sort Paper 2 · Algorithms, Programming and Logic
By the end of this lesson you can:
- Describe and write a linear search that uses a
Foundflag and reports the position. - Describe and write a bubble sort that stops early when a pass makes no swaps.
- Trace both algorithms with a trace table.
Textbook: Chapter 7, §7.4.4–7.4.5 (pp. 274–276)
Recap / Warm-Up 5 min
Last lesson you met totalling, counting and max/min. The last two standard methods look for a value, and put values in order.
Quick starter
You are looking for your friend's name on an unsorted list of 200 names. What is the most names you might have to read?
Reveal the answer
All 200 — if the name is last, or not there at all. That is the worst case for a linear search.
🧠 Key Concept 14 min
1 · Linear search
A linear search inspects each item in a list in turn, from the first, to see if it matches the value searched for. It stops when it finds a match or reaches the end of the list. The list does not need to be sorted.
A Boolean flag, Found, starts as FALSE and becomes TRUE when a match is found. The loop stops when Found is true or the counter passes the end.
A variation keeps searching to the end to count every match — for example, how many students chose "pizza" as their favourite lunch. That uses a FOR loop and no flag.
2 · Bubble sort
A bubble sort compares each element with the next one and swaps them if they are in the wrong order, from the first element to the next-to-last. After one pass, the largest value has "bubbled" to the end and is in its final place.
Each later pass compares one fewer pair. The sort stops when a pass makes no swaps (the list is already in order) or when only one element is left to check.
Temp. Without it, the first value would be overwritten and lost.Worked Example 12 min
Worked example 1 · Linear search for a name
The array StudentName[1:5] holds Ava, Leo, Mia, Kai, Zoe. Search for Kai.
OUTPUT "Please enter name to find "
INPUT Name
Found ← FALSE
Counter ← 1
REPEAT
IF Name = StudentName[Counter]
THEN
Found ← TRUE
ELSE
Counter ← Counter + 1
ENDIF
UNTIL Found OR Counter > ClassSize
IF Found
THEN
OUTPUT Name, " found at position ", Counter
ELSE
OUTPUT Name, " not found."
ENDIFstudent_name = ["Ava", "Leo", "Mia", "Kai", "Zoe"] name = input("Please enter name to find ") found = False counter = 0 while not found and counter < len(student_name): if name == student_name[counter]: found = True else: counter = counter + 1 if found: print(name, "found at position", counter + 1) else: print(name, "not found.")
Please enter name to find Kai Kai found at position 4
| Name | Counter | StudentName[Counter] | Found | OUTPUT |
|---|---|---|---|---|
| Kai | 1 | FALSE | Please enter name to find | |
| 2 | Ava | |||
| 3 | Leo | |||
| 4 | Mia | |||
| Kai | TRUE | Kai found at position 4 |
- Set up:
Found ← FALSE,Counter ← 1.the flag must start false, or the loop would stop at once. - Positions 1–3 miss, so Counter goes 2, 3, 4.
- Position 4 matches:
Found ← TRUE. Counter is not increased, so it still holds the position, 4.that is why the counter goes in theELSEpath. - Search for Sam instead: all five miss, Counter reaches 6,
6 > 5ends the loop, and the output is Sam not found.
Worked example 2 · Bubble sort of five temperatures
Sort Temperature[1:5] = 31, 25, 38, 22, 29 into ascending order.
First ← 1
Last ← 5
REPEAT
Swap ← FALSE
FOR Index ← First TO Last - 1
IF Temperature[Index] > Temperature[Index + 1]
THEN
Temp ← Temperature[Index]
Temperature[Index] ← Temperature[Index + 1]
Temperature[Index + 1] ← Temp
Swap ← TRUE
ENDIF
NEXT Index
Last ← Last - 1
UNTIL (NOT Swap) OR Last = 1temperature = [31, 25, 38, 22, 29] last = len(temperature) - 1 swap = True while swap and last > 0: swap = False for index in range(last): if temperature[index] > temperature[index + 1]: temp = temperature[index] temperature[index] = temperature[index + 1] temperature[index + 1] = temp swap = True last = last - 1 print(temperature)
[22, 25, 29, 31, 38]
| Last | Swap | Index | Temp | T[1] | T[2] | T[3] | T[4] | T[5] |
|---|---|---|---|---|---|---|---|---|
| 5 | 31 | 25 | 38 | 22 | 29 | |||
| FALSE | ||||||||
| TRUE | 1 | 31 | 25 | 31 | ||||
| 2 | ||||||||
| 3 | 38 | 22 | 38 | |||||
| 4 | 38 | 29 | 38 | |||||
| 4 | FALSE | 1 | ||||||
| TRUE | 2 | 31 | 22 | 31 | ||||
| 3 | 31 | 29 | 31 | |||||
| 3 | FALSE | |||||||
| TRUE | 1 | 25 | 22 | 25 | ||||
| 2 | ||||||||
| 2 | FALSE | 1 | ||||||
| 1 |
- Pass 1 (Last = 5) compares 4 pairs and makes 3 swaps. 38 reaches position 5.
- Pass 2 (Last = 4) compares 3 pairs, 2 swaps: 25, 22, 29, 31, 38. 31 is now fixed.
Last ← Last - 1skips positions already in their final place. - Pass 3 (Last = 3) swaps 25 and 22: 22, 25, 29, 31, 38.
- Pass 4 (Last = 2) compares 22 and 25 — no swap.
Swapstays FALSE, soNOT Swapis TRUE and the sort stops.a pass with no swaps proves the list is sorted, so more passes would waste time.
Try It Yourself 12 min
Goal: Using worked example 1, state how many names are compared when searching for (a) Ava, (b) Zoe, (c) Ben.
Goal: Write the list after each pass of a bubble sort on 8, 3, 6, 1, 9. How many passes are needed before the sort stops?
Goal: Amend the bubble sort so it sorts StudentName[1:30] into reverse alphabetical order (Z to A). Then write it in Python.
Hint
Only two things change: the array name and bounds, and the comparison sign. Which way round must the pair be for a swap when sorting Z to A?
📝 Exam Practice 10 min
Describe how a linear search finds a value in a list.
Mark scheme
- Starts at the first item in the list (1).
- Compares each item in turn with the search value (1).
- Stops when a match is found / outputs its position (1).
- …or when the end of the list is reached / outputs not found (1). Max 3.
Describe how a bubble sort puts a list of numbers into ascending order.
Mark scheme
- Each element is compared with the next element (1).
- If the first is larger, the two are swapped (1).
- This is repeated from the first to the next-to-last element — one pass (1).
- Passes are repeated (each one shorter, as the last element is in place) (1)…
- …until a pass makes no swaps (1). Max 4.
Show the contents of the list 14, 9, 17, 3, 11 after the first pass of a bubble sort into ascending order.
Mark scheme
- 9, 14, 3, 11, 17 (2).
- One mark if 17 is correctly in last place but the rest is wrong (1).
The array Code[1:50] holds product codes. Write pseudocode to input a code, search the array, and output its position or "Not found".
Mark scheme
INPUT SearchCodeandFound ← FALSE/ counter set to 1 (1)- A loop through the array, e.g.
REPEAT … UNTIL Found OR Index > 50(1) - Comparison
IF Code[Index] = SearchCode(1) - Flag set / index increased correctly (1)
- Correct output of the position or "Not found" after the loop (1)
🗝️ Recap & Key Terms 3 min
A linear search checks each item in turn until it finds a match or reaches the end; a Boolean flag records success. A bubble sort compares neighbours and swaps them, pass after pass, until a pass makes no swaps.
- Linear search
- An algorithm that inspects each item in a list in turn to see if it matches the value searched for.
- Bubble sort
- An algorithm that makes repeated passes through a list, comparing each element with the next and swapping them if needed, until a pass makes no swaps.
- Flag
- A Boolean variable (TRUE/FALSE) that records whether something has happened, e.g.
FoundorSwap. - Pass
- One run through the list from the first element to the last unsorted element.
- Temporary variable
- A variable (
Temp) used to hold one value while two values are swapped.
Homework 1 min
Task (≤ 15 min): A list holds the favourite fruits of 25 students in Fruit[1:25]. Write pseudocode that inputs a fruit name and outputs how many students chose it. Explain why a FOR loop is used instead of REPEAT…UNTIL Found. [5]
Model answer
OUTPUT "Which fruit? "
INPUT Choice
ChoiceCount ← 0
FOR Index ← 1 TO 25
IF Fruit[Index] = Choice
THEN
ChoiceCount ← ChoiceCount + 1
ENDIF
NEXT Index
OUTPUT ChoiceCount, " chose ", ChoiceMarks: input (1); count initialised (1); FOR loop over all 25 (1); comparison and count increase (1). Reason: several students can choose the same fruit, so every item must be checked — stopping at the first match would undercount (1).