For AS students extending their foundations and A2 students developing advanced theory and practical programming. Choose either stage or the full A Level; O Level is a separate qualification.
Step 1 of 6. Start at index 1 with Found = FALSE. Search for 31 from left to right.
Read the pseudocode
Cambridge-style pseudocode; 0 means not found. A comparison counts one array item inspected against the target, not each Boolean test. Each diagram step records one inspection, or the stopping condition.
// Values is a global array, populated as shown above.
DECLARE Values : ARRAY[1:6] OF INTEGER
FUNCTION LinearSearch(Target : INTEGER) RETURNS INTEGER
DECLARE Index : INTEGER
DECLARE Found : BOOLEAN
Index ← 1
Found ← FALSE
WHILE (Index <= 6) AND (NOT Found)
IF Values[Index] = Target THEN
Found ← TRUE
ELSE
Index ← Index + 1
ENDIF
ENDWHILE
IF Found THEN
RETURN Index
ELSE
RETURN 0
ENDIF
ENDFUNCTION
// Example call: OUTPUT LinearSearch(31)
Read the complete trace
Complete trace · target 31 · one-based indices
Step
Compared index
Comparisons
Found
Reasoning
1 · current
—
0
FALSE
Start at index 1 with Found = FALSE. Search for 31 from left to right.
2
1
1
FALSE
Compare index 1: 18 ≠ 31. Found stays FALSE; advance Index to 2.
3
2
2
FALSE
Compare index 2: 7 ≠ 31. Found stays FALSE; advance Index to 3.
4
3
3
FALSE
Compare index 3: 25 ≠ 31. Found stays FALSE; advance Index to 4.
5
4
4
FALSE
Compare index 4: 12 ≠ 31. Found stays FALSE; advance Index to 5.
6
5
5
TRUE
Compare index 5: 31 = 31. Set Found = TRUE and stop; return index 5.
Your turn: search the same array for 12. Which index is returned, how many comparisons occur, and why does the loop stop?
Reveal practice answer
Index 4, after four comparisons. Found becomes TRUE when Values[4] = 12, so the loop stops without checking indices 5 and 6.
A2: recursive binary search
Learning outcome: trace bounds and recursive calls on a sorted array, including the empty-range base case.
Step 1 of 3. The array is sorted ascending. Start BinarySearch(1, 7, 42); test Low > High before reading a midpoint.
Recursive call sequence (Low, High, Target)
No calls evaluated yet.
Read the pseudocode
Cambridge-style pseudocode; 0 means not found. A comparison counts one array item inspected against the target, not each Boolean test. Each diagram step records one inspection, or the stopping condition.
// Values is global, populated in ascending order as above.
DECLARE Values : ARRAY[1:7] OF INTEGER
FUNCTION BinarySearch(Low : INTEGER, High : INTEGER,
Target : INTEGER) RETURNS INTEGER
DECLARE Mid : INTEGER
IF Low > High THEN
RETURN 0
ENDIF
Mid ← (Low + High) DIV 2
IF Values[Mid] = Target THEN
RETURN Mid
ENDIF
IF Values[Mid] < Target THEN
RETURN BinarySearch(Mid + 1, High, Target)
ELSE
RETURN BinarySearch(Low, Mid - 1, Target)
ENDIF
ENDFUNCTION
// Example call: OUTPUT BinarySearch(1, 7, 42)
Read the complete trace
Complete trace · target 42 · one-based indices
Step
Low / Mid / High
Comparisons
Found
Reasoning
1 · current
1 / — / 7
0
FALSE
The array is sorted ascending. Start BinarySearch(1, 7, 42); test Low > High before reading a midpoint.
2
1 / 4 / 7
1
FALSE
Call 1: Mid = (1 + 7) DIV 2 = 4. Compare 23 with 42. Discard Mid and everything below it; next call uses Low 5, High 7.
3
5 / 6 / 7
2
TRUE
Call 2: Mid = (5 + 7) DIV 2 = 6. Compare 42 with 42. Match: return index 6 through all waiting calls.
Your turn: search the same sorted array for 15. List each (Low, Mid, High) triple and the returned index.
Reveal practice answer
(1, 4, 7), then (1, 2, 3), then (3, 3, 3). Return index 3 after three comparisons; that result passes back through the waiting calls.
03 / HOW WE’LL LEARN
A starting point. A clear next step.
We identify your AS or A2 stage and current difficulties before choosing problems. Trace algorithms, explain decisions and attempt independent solutions, with time to run, test and debug code during practical programming sessions.