Standard algorithms
Introduzido| English | Significado |
|---|---|
| linear search/ˈlɪnɪə sɜːtʃ/ | busca linear |
| bubble sort/ˈbʌbl sɔːt/ | ordenação por bolha |
| counting/ˈkaʊntɪŋ/ | contagem |
| totalling/ˈtəʊtəlɪŋ/ | soma acumulada |
The classic recipes
- 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
DECLARE Found : BOOLEAN
Found ← FALSE
FOR I ← 1 TO 10
IF List[I] = SearchValue
THEN
Found ← TRUE
ENDIF
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.

Standard algorithms
compare adjacent, swap if needed
Step through a bubble sort — compare neighbours and swap until everything is in order.
A linear search finds a value by:
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
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
ENDIF
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.

A bubble sort puts a list in order by:
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
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.

Which line adds a value to a running total?
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
Total ← Total + Value accumulates a sum; Count ← Count + 1 counts occurrences.
Match each standard algorithm to how it works.
Dificuldade 2/5 · Moedas 140 + pequeno bônus na primeira conclusão hoje
These four — search, sort, total and max — are the building blocks of most exam algorithms.
Using totalling on the list [2, 4, 6, 8], what is the final total?
Dificuldade 2/5 · Moedas 140 + pequeno bônus na primeira conclusão hoje
2 + 4 + 6 + 8 = 20.
Worked example: one pass of bubble sort
Show the first pass of a bubble sort on 5 2 8 1, sorting into ascending order.
- Compare 5 and 2: swap →
2 5 8 1. Compare 5 and 8: no swap. Compare 8 and 1: swap →2 5 1 8. After one pass the largest value, 8, is in its final place. - More passes are needed: pass 2 gives
2 1 5 8, pass 3 gives1 2 5 8. A pass with no swaps shows the list is sorted, so the algorithm stops.
A linear search looks for 7 in the list 4 9 2 7 5. How many items does it compare before it finds 7, including 7 itself?
Dificuldade 2/5 · Moedas 140 + pequeno bônus na primeira conclusão hoje
It checks 4, 9, 2, then 7: four comparisons.
A program has totalled 5 marks to get 340. What is the average?
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
Average = total ÷ count = 340 ÷ 5 = 68.
You've got it
- 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