[7.3] Explain algorithms
Explaining Algorithms: Purpose and Processes
From problem to precise, step-by-step solutions
An algorithm is a finite, ordered set of steps that solves a problem. When an exam question says “explain the purpose and processes of a given algorithm”, you must clearly state what the algorithm achieves (its purpose) and how it achieves it (its process or logic). Good explanations identify inputs and outputs, describe the control flow (sequence, selection, and iteration), and show the effect on data as the algorithm runs. Where helpful, you should perform a short dry run with typical, boundary, and erroneous data to demonstrate understanding.
Think of an algorithm as a recipe: purpose is “bake a sponge cake”, while process is the specific sequence of measuring, mixing, and baking. In computing we describe processes using flowcharts and pseudocode, and sometimes include a reference implementation (e.g. Python) to consolidate understanding.
How to explain an algorithm
A four-part framework
- State the purpose: a one-sentence summary such as “This algorithm searches a list for a target value and returns its index if found, otherwise −1”.
- Specify the interface: inputs (names and data types), outputs/side effects, and any assumptions or preconditions (e.g. “the list is sorted ascending”).
- Describe the process: walk through the main loop and decisions. Name the variables and explain when they change.
- Demonstrate with a dry run: show how variables evolve for at least one normal case and one edge case (e.g. target at first or last position, not present).
Worked examples with contrasts
Reading and explaining common algorithms
Below are tabbed examples that highlight the same concept across contrasting scenarios. Notice how the explanation structure stays consistent while details change.
Searching a list: purpose, process, and assumptions
Purpose: find a target value in an unsorted list by checking each element in turn.
Process: start at index 0, compare the current element to the target, and either return the index (if equal) or move to the next element. Stop after the last element and report “not found”.
Dry run (list = [8, 3, 12, 5], target = 12): compare 8 (no), compare 3 (no), compare 12 (yes) → return 2.
// CAIE Pseudocode: Linear Search
// Returns index if found, otherwise -1
FUNCTION LinearSearch(list: ARRAY OF INTEGER, target: INTEGER) RETURNS INTEGER
FOR i ← 0 TO LENGTH(list) - 1
IF list[i] = target THEN
RETURN i
ENDIF
NEXT i
RETURN -1
ENDFUNCTION
# Python: Linear search
def linear_search(items, target):
for i, v in enumerate(items):
if v == target:
return i
return -1
Purpose: find a target value in a sorted list efficiently by repeatedly halving the search range.
Process: set low and high to the first and last indices; find mid; if target is equal to the middle element return its index; if target is smaller, search the left half; otherwise search the right half. Repeat until found or the range is empty.
Assumption: the list is sorted ascending. Without this, the process fails to work correctly.
// CAIE Pseudocode: Binary Search (iterative)
FUNCTION BinarySearch(list: ARRAY OF INTEGER, target: INTEGER) RETURNS INTEGER
DECLARE low INTEGER ← 0
DECLARE high INTEGER ← LENGTH(list) - 1
WHILE low ≤ high DO
DECLARE mid INTEGER ← (low + high) DIV 2
IF list[mid] = target THEN
RETURN mid
ELSEIF target < list[mid] THEN
high ← mid - 1
ELSE
low ← mid + 1
ENDIF
ENDWHILE
RETURN -1
ENDFUNCTION
# Python: Binary search (iterative)
def binary_search(items, target):
low, high = 0, len(items) - 1
while low <= high:
mid = (low + high) // 2
if items[mid] == target:
return mid
elif target < items[mid]:
high = mid - 1
else:
low = mid + 1
return -1
Edge case: when the value is not present, both algorithms traverse until their stopping condition and return −1. Examiners expect you to mention this explicit outcome, not just describe the successful path.
Dry run (linear search; list = [2, 4, 6], target = 5): compare 2, 4, 6 → none match → return −1. For binary search with [2, 4, 6], target = 5: low=0, high=2, mid=1 (value 4, target greater) → low=2; mid=2 (value 6, target smaller) → high=1; low > high → return −1.
Explaining algorithms that modify data
Example: Bubble Sort with contrasts
Some algorithms transform data structures. For sorting, a clear explanation should include the invariant (what remains true after each pass), the swapping step, and a halting condition.
Purpose: reorder a list into ascending order.
Process: repeatedly pass through the list, comparing adjacent elements and swapping if they are out of order. After each full pass, the largest unsorted value has “bubbled” to the end.
// CAIE Pseudocode: Bubble Sort (basic)
PROCEDURE BubbleSort(list: ARRAY OF INTEGER)
FOR pass ← 0 TO LENGTH(list) - 2
FOR i ← 0 TO LENGTH(list) - 2 - pass
IF list[i] > list[i+1] THEN
// swap
DECLARE temp INTEGER ← list[i]
list[i] ← list[i+1]
list[i+1] ← temp
ENDIF
NEXT i
NEXT pass
ENDPROCEDURE
# Python: Bubble sort (basic)
def bubble_sort(a):
n = len(a)
for p in range(n - 1):
for i in range(n - 1 - p):
if a[i] > a[i + 1]:
a[i], a[i + 1] = a[i + 1], a[i]
Optimisation: track whether a swap occurred in the current pass. If no swaps occur, the list is already sorted and we can stop early.
Why it matters: improves best-case performance (already-sorted input) by avoiding unnecessary passes.
Common mistake: looping the inner index to LENGTH(list) - 1 - pass and then accessing list[i+1] causes an out-of-range access. The correct bound is LENGTH(list) - 2 - pass for the inner loop when comparing i and i+1. Always align the loop limit with the largest index you will touch.
Dry runs and trace tables
Show variable changes step by step
Trace tables help you justify your explanation. Include columns for the loop counters, key variables, and outputs. Record values after each important step or iteration. You do not need every minor change—focus on the variables that drive decisions.
| Step | i | Current value | Compared to | Action | Output/State |
|---|---|---|---|---|---|
| 1 | 0 | 8 | target 12 | No match | continue |
| 2 | 1 | 3 | target 12 | No match | continue |
| 3 | 2 | 12 | target 12 | Match | return 2 |
Clarity checklist for your explanations
What examiners look for
- Purpose is stated precisely, including the “not found” or error behaviour if relevant.
- Inputs and outputs are named and typed; any preconditions (e.g. “sorted list”) are explicit.
- Process covers the main loop and key decisions succinctly, using correct terminology.
- Dry run demonstrates understanding on normal and edge cases.
- Correctness conditions (e.g. loop bounds, swap positions) are accurate; common mistakes are avoided.
Key Takeaways
- Always separate purpose (what it does) from process (how it works).
- Name inputs/outputs and state any assumptions such as sorted data.
- Explain control flow using precise terms: initialise, compare, swap, increment, return.
- Use a concise dry run to evidence understanding, including a not-present edge case.
- Watch for off-by-one and boundary errors when explaining loops and indices.