7.4 Standard methods of solution

← Topic 7.3 Explaining the purpose of an algorithmComputer Science contentsTopic 7.5 Validation and verification →
Chapter 7 · Algorithm design and problem solving

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.

TotallingCountingMaximum / minimum / averageLinear searchBubble 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.

MethodWhat it does
TotallingKeeps a running total by repeatedly adding values.
CountingKeeps track of how many times an event or condition occurs.
Maximum / minimum / averageFinds extreme values or calculates a mean from a list.
Linear searchChecks items one by one until a match is found or the list ends.
Bubble sortRepeatedly 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.

Important pattern: initialise the total before the loop, then update it inside the loop. If the total were reset to zero inside the loop, the previous values would be lost.
Check the totalling pattern.

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 Counter

PassCount 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()
ENDIF

Each sale reduces the stock count by one. If the value drops below 20, the reorder routine is called.

Check counting up and counting down.

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 Counter

Whenever 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 Counter

Calculating 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
Remember: average = total of all values ÷ number of values. The division is done after the loop has finished totalling all the values.
Check maximum, minimum and average.

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."
ENDIF

Found 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."
SituationApproach
Find one unique itemStop when the item is found or the list ends.
Count all matching itemsCheck every item and increment a counter for each match.
Check linear-search logic.

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

  1. Start at the first item.
  2. Compare it with the next item.
  3. If the pair is in the wrong order, swap them.
  4. Move to the next pair and repeat.
  5. After one complete pass, the final item is in its correct position.
  6. Repeat the process on the remaining unsorted part of the list.
  7. 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 = 1

The 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.
Check bubble-sort logic.

Topic 7.4 revision checklist

Use a running total and initialise it before the loop.
Use counters to record how many times an event occurs.
Recognise that counters can increase or decrease.
Find maximum and minimum values by updating stored extreme values.
Initialise maximum/minimum using known limits or the first list item.
Calculate an average by totalling the values then dividing by the number of values.
Explain how a linear search checks list items one by one.
Use a Boolean flag such as Found to record whether a match has been located.
Distinguish finding one match from counting every matching value.
Explain how bubble sort compares adjacent items and swaps them when required.
Explain why the last item is in the correct position after each bubble-sort pass.
Recognise the role of temporary and Boolean flag variables in bubble sort.
Ready for a mixed Topic 7.4 check?
← Topic 7.3 Explaining the purpose of an algorithmComputer Science contentsTopic 7.5 Validation and verification →