Standard algorithms
Standard algorithms
- A few standard algorithms appear again and again.
- You must know linear search, bubble sort, and the counting/totalling patterns.
- Each is short, but worth recognising instantly.
Linear search
found ← FALSE
FOR i ← 0 TO 9
IF list[i] = searchValue THEN found ← TRUE
NEXT i
OUTPUT found
- A linear search checks each item in turn, from the start, until it finds the value (or reaches the end).
- It works on any list — no sorting needed.
Practice
A linear search finds a value by:
Linear search examines items one by one until it finds the value or reaches the end.
Bubble sort
FOR i ← 0 TO 8
IF list[i] > list[i + 1] THEN
temp ← list[i]
list[i] ← list[i + 1]
list[i + 1] ← temp
NEXT i
- A bubble sort compares each side-by-side pair and swaps them if they are out of order.
- It repeats this until no more swaps are needed — leaving the list sorted.
Practice
A bubble sort puts a list in order by:
It swaps out-of-order neighbours and repeats passes until no swaps are needed.
Totalling, counting, max/min/average
- Totalling — keep a running total:
total ← total + value. - Counting — add 1 each time something happens:
count ← count + 1. - Maximum/minimum — keep the largest/smallest value seen so far.
- Average — divide the total by how many values there are.
Practice
Which line adds a value to a running total?
total ← total + value accumulates a sum; count ← count + 1 counts occurrences.
Practice
To find the maximum value in a list, you:
Track the biggest value seen so far, updating it whenever a larger one appears.
Practice
Using totalling on the list [2, 4, 6, 8], what is the final total?
2 + 4 + 6 + 8 = 20.
You've got it
Key idea
- linear search checks each item in turn (works on any list)
- bubble sort swaps out-of-order neighbours, repeating until sorted
- totalling (
total ← total + value) and counting (count ← count + 1) - max/min = keep the best so far; average = total ÷ count