7 Algorithm design and problem-solving

Show All Section Notes

Standard Methods of Solution

1. What Is a Standard Method of Solution?

Certain small tasks appear again and again inside larger programs: adding up a list, counting how many items match a condition, finding the biggest value, searching for an item, or putting data in order. These recurring solutions are called standard methods of solution. Learning them means you never have to invent them under exam pressure.

The syllabus limits this to six methods: linear search, bubble sort, totalling, counting, and finding maximum, minimum and average values. You are expected to recognise them, explain them, and write them.

2. Totalling

Totalling means adding every value in a set of data to produce a running sum.

The essential detail is that the total must be set to 0 before the loop starts. Initialising it inside the loop resets it on every pass and the answer is always the last value.

DECLARE Total : INTEGER DECLARE Number : INTEGER Total ← 0 // MUST be outside the loop FOR Count ← 1 TO 10 OUTPUT "Enter a number: " INPUT Number Total ← Total + Number NEXT Count OUTPUT "The total is ", Total

3. Counting

Counting means keeping track of how many items there are, or how many meet a condition. Note the difference from totalling: totalling adds the values, counting adds 1.

DECLARE PassCount : INTEGER PassCount ← 0 FOR Count ← 1 TO 30 INPUT Mark IF Mark >= 50 THEN PassCount ← PassCount + 1 // add 1, not the mark ENDIF NEXT Count OUTPUT PassCount, " students passed"
Totalling versus counting is a classic trap. If a question asks how many students scored over 50, the answer is a count (+ 1). If it asks for the combined score of those students, that is a total (+ Mark). Read which one is wanted.

4. Finding Maximum and Minimum

To find the largest value, hold a "best so far" variable and replace it whenever a bigger value appears.

DECLARE Highest : INTEGER DECLARE Lowest : INTEGER INPUT Number Highest ← Number // start both at the FIRST value Lowest ← Number FOR Count ← 2 TO 10 INPUT Number IF Number > Highest THEN Highest ← Number ENDIF IF Number < Lowest THEN Lowest ← Number ENDIF NEXT Count OUTPUT "Highest: ", Highest OUTPUT "Lowest: ", Lowest
Do not initialise Highest to 0. If every value is negative, the answer stays 0, which is wrong. Setting it to 0 also fails a boundary check. Start from the first data item instead, then loop from the second.

5. Finding an Average

An average combines totalling and counting: total the values, count them, then divide.

Total ← 0 Count ← 0 REPEAT INPUT Number IF Number <> -1 THEN Total ← Total + Number Count ← Count + 1 ENDIF UNTIL Number = -1 // -1 is the rogue value IF Count > 0 THEN // guard against dividing by zero OUTPUT "Average: ", Total / Count ELSE OUTPUT "No data entered" ENDIF
A rogue value (or sentinel) is a value outside the valid range used to signal the end of input. It must not be included in the total or the count.

6. Linear Search

A linear search checks each item in turn, from the start, until the target is found or the data runs out.

DECLARE Names : ARRAY[1:20] OF STRING DECLARE Found : BOOLEAN DECLARE Index : INTEGER Found ← FALSE Index ← 1 WHILE Index <= 20 AND Found = FALSE IF Names[Index] = SearchName THEN Found ← TRUE ELSE Index ← Index + 1 ENDIF ENDWHILE IF Found = TRUE THEN OUTPUT "Found at position ", Index ELSE OUTPUT "Not found" ENDIF
AdvantagesDisadvantages
The data does not need to be sorted firstSlow on large data sets
Simple to write and understandMay have to check every item to find nothing
Use a flag and stop early. A search that keeps looping after the item is found still "works" but wastes effort, and answers that never stop searching are penalised. The Found = FALSE condition in the WHILE is what stops it.

7. Bubble Sort

A bubble sort repeatedly compares neighbouring pairs and swaps them if they are in the wrong order. After each pass the largest remaining value has "bubbled" to the end.

DECLARE Numbers : ARRAY[1:6] OF INTEGER DECLARE Temp : INTEGER FOR Pass ← 1 TO 5 FOR Index ← 1 TO 5 IF Numbers[Index] > Numbers[Index + 1] THEN Temp ← Numbers[Index] // 3-step swap Numbers[Index] ← Numbers[Index + 1] Numbers[Index + 1] ← Temp ENDIF NEXT Index NEXT Pass

Worked trace of one pass

Starting data: 5, 3, 8, 1

CompareSwap?List after
5 and 3Yes, 5 > 33, 5, 8, 1
5 and 8No3, 5, 8, 1
8 and 1Yes, 8 > 13, 5, 1, 8

The largest value, 8, is now at the end. That is guaranteed after every pass, which is why the sort works.

The swap needs three steps and a temporary variable. Writing A ← B then B ← A destroys the first value — both end up the same. Marks are routinely lost here. Always: save to Temp, copy across, restore from Temp.

8. Exam Focus

Initialise before the loop, not inside it. Totals and counters set to 0 inside the loop are the most frequent single error in this topic.
Be precise in written answers. "It compares the numbers" is not enough for a bubble sort. Say that it compares adjacent or neighbouring items and swaps them if they are in the wrong order, repeating until no swaps are needed.

Quick self-check

  • State the difference between totalling and counting.
  • Explain why Highest should not start at 0.
  • Write the three lines needed to swap two array elements.
  • Give one advantage and one disadvantage of a linear search.
  • After one full pass of a bubble sort, which value is certainly in its final position?