7.4 Standard methods of solution
Many algorithms reuse the same reliable patterns. Topic 7.4 focuses on five standard methods you must be able to understand and use: totalling, counting, finding maximum/minimum/average values, linear search and bubble sort.
Why standard methods matter
When designing an algorithm, it is often unnecessary to invent every step from scratch. Standard methods are familiar algorithm patterns that solve common problems. Once turned into a program, the same pattern may be used many times with different data.
The textbook requires you to understand and use five methods. Each is shown using pseudocode so that you can recognise the pattern and later translate it into a programming language.
| Method | What it does |
|---|---|
| Totalling | Keeps a running total by repeatedly adding values. |
| Counting | Keeps track of how many times an event or condition occurs. |
| Maximum / minimum / average | Finds extreme values or calculates a mean from a list. |
| Linear search | Checks items one by one until a match is found or the list ends. |
| Bubble sort | Repeatedly compares neighbouring items and swaps those in the wrong order. |
7.4.1 Totalling
Totalling means maintaining a running total. A total variable is normally initialised to zero before the loop begins, then each new value is added to the existing total.
Example: total the marks in a class
Total ← 0 FOR Counter ← 1 TO ClassSize Total ← Total + StudentMark[Counter] NEXT Counter
The algorithm starts with Total = 0. Each pass through the loop adds one student mark. After the final iteration, Total contains the sum of all marks.
7.4.2 Counting
Counting records how many times something happens. A counter is normally initialised before the loop and changed whenever the event being counted occurs.
Example: count students who pass
PassCount ← 0
FOR Counter ← 1 TO ClassSize
INPUT StudentMark
IF StudentMark > 50
THEN
PassCount ← PassCount + 1
ENDIF
NEXT CounterPassCount only increases when the condition is satisfied. The value therefore represents the number of marks above 50.
Counting down
A counter does not always increase. It can decrease when an item is used or removed. The textbook gives a stock-control example:
NumberInStock ← NumberInStock - 1
IF NumberInStock < 20
THEN
CALL Reorder()
ENDIFEach sale reduces the stock count by one. If the value drops below 20, the reorder routine is called.
7.4.3 Maximum, minimum and average
Finding a maximum and minimum
To find the largest and smallest values in a list, the algorithm keeps current maximum and minimum values and compares every list item with them.
Method 1: known possible limits
If marks are known to be in the range 0 to 100, the maximum can begin at the lowest possible mark and the minimum at the highest possible mark:
MaximumMark ← 0
MinimumMark ← 100
FOR Counter ← 1 TO ClassSize
IF StudentMark[Counter] > MaximumMark
THEN
MaximumMark ← StudentMark[Counter]
ENDIF
IF StudentMark[Counter] < MinimumMark
THEN
MinimumMark ← StudentMark[Counter]
ENDIF
NEXT CounterWhenever a higher value is found, it replaces the current maximum. Whenever a lower value is found, it replaces the current minimum.
Method 2: limits not known
If the possible range is not known, initialise both values to the first item and start comparisons from the second:
MaximumMark ← StudentMark[1]
MinimumMark ← StudentMark[1]
FOR Counter ← 2 TO ClassSize
IF StudentMark[Counter] > MaximumMark
THEN
MaximumMark ← StudentMark[Counter]
ENDIF
IF StudentMark[Counter] < MinimumMark
THEN
MinimumMark ← StudentMark[Counter]
ENDIF
NEXT CounterCalculating an average (mean)
The average uses the totalling method first, then divides by the number of values:
Total ← 0 FOR Counter ← 1 TO ClassSize Total ← Total + StudentMark[Counter] NEXT Counter Average ← Total / ClassSize
7.4.4 Linear search
A linear search works through a list one item at a time. Each item is compared with the value being searched for. The search can stop as soon as a match is found, or after every item has been checked.
Example: find a name in a class list
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, " in the list."
ELSE
OUTPUT Name, " not found."
ENDIFFound is a Boolean flag. It starts as FALSE and becomes TRUE when the item is found. The loop stops either when a match is found or when the end of the list has been passed.
Counting repeated matches
If more than one item may match, the algorithm should not stop after the first match. The textbook gives an example that counts how many people chose ice cream:
ChoiceCount ← 0
FOR Counter ← 1 TO Length
IF "ice cream" = Dessert[Counter]
THEN
ChoiceCount ← ChoiceCount + 1
ENDIF
NEXT Counter
OUTPUT ChoiceCount, " chose ice cream as their favourite dessert."| Situation | Approach |
|---|---|
| Find one unique item | Stop when the item is found or the list ends. |
| Count all matching items | Check every item and increment a counter for each match. |
7.4.5 Bubble sort
A bubble sort puts list items into order by repeatedly comparing neighbouring items. If two adjacent items are in the wrong order, they are swapped.
How the method works
- Start at the first item.
- Compare it with the next item.
- If the pair is in the wrong order, swap them.
- Move to the next pair and repeat.
- After one complete pass, the final item is in its correct position.
- Repeat the process on the remaining unsorted part of the list.
- Stop when no swaps are made or only one item remains to check.
Example: sort ten temperatures into ascending order
First ← 1
Last ← 10
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 = 1The variable Temp temporarily stores one value while the two neighbouring items are exchanged. Swap is a Boolean flag. If an entire pass makes no swaps, the list is already sorted and the algorithm can stop early.
Mini trace
For the list [7, 3, 5]:
- Compare 7 and 3 → swap →
[3, 7, 5] - Compare 7 and 5 → swap →
[3, 5, 7] - The largest value, 7, has reached the end after the first pass.
- A later pass confirms that no further swap is needed.