SDD  ·  Software Design & Development

Standard Algorithms: Min, Max, Count

Lesson 14 of 18 Approx 60 min Requires: SDD12 Arrays, SDD9 Fixed Loops, SDD7 Selection
Learning intentions
  • Understand what a standard algorithm is and why the SQA specification requires them
  • Apply the find-minimum and find-maximum algorithms to arrays using loops and selection
  • Apply the count occurrences algorithm to count how many array elements meet a condition
Success criteria
  • I can explain why the minimum variable must be initialised to array[0], not 0
  • I can write Python code to find the minimum value in an array and trace it step by step
  • I can write Python code to find the maximum value in an array and trace it step by step
  • I can write Python code to count how many array elements satisfy a given condition
  • I can write SQA pseudocode for all three algorithms using correct loop bounds and keywords
  • I can identify and correct common errors in minimum, maximum, and count algorithms
Warm up — what do you already know?

Answer before the lesson begins. These check prior knowledge from earlier lessons — it's fine if you're unsure.

1. An array is declared as scores = [45, 78, 62, 91, 30]. What is the value of scores[4]?

2. What must be done to a variable (such as total) before it is used inside a loop to accumulate a running total?

3. The array scores = [45, 78, 62, 91, 30] is traversed and the condition scores[i] >= 60 is checked each time. How many times does the condition evaluate to True?

Key vocabulary

standard algorithm
A recognised, well-tested method for solving a common programming problem. The SQA N5 specification requires you to implement find-minimum, find-maximum, count occurrences, and linear search.
minimum
The smallest value in a dataset. Found by comparing each array element in turn and keeping track of the smallest seen so far.
maximum
The largest value in a dataset. Found using the same structure as the minimum algorithm, but the comparison is reversed: > instead of <.
count
The number of elements in an array that satisfy a given condition. A counter variable starts at 0 and is incremented each time the condition is True.
initialise
To set a variable to a known starting value before a loop begins. For minimum/maximum, initialise to the first element. For a counter, initialise to 0.
traverse
To visit every element of an array in sequence, usually using a FOR loop. All three algorithms in this lesson require a full traversal of the array.

Standard Algorithms: Min, Max, Count

What is a standard algorithm?

A standard algorithm is a well-known, reusable pattern for solving a common problem in programming. Rather than inventing a new approach every time, programmers recognise the pattern and apply it. The SQA N5 specification explicitly lists four standard algorithms that you must be able to implement and explain: find minimum, find maximum, count occurrences, and linear search. This lesson covers the first three; linear search is covered in SDD13.

All three algorithms in this lesson share the same underlying structure: set up a variable before the loop, traverse the array element by element, update the variable inside the loop based on a condition, and use the result after the loop ends.

Find minimum

The find-minimum algorithm locates the smallest value in an array. The key steps are:

  1. Initialise a variable called minimum to the value of the first element of the array: minimum = array[0].
  2. Loop from index 1 to the last index (you have already accounted for index 0).
  3. Compare each element to minimum. If the element is smaller, update minimum to that element's value.
  4. After the loop, minimum holds the smallest value in the array.
temps = [18, 12, 25, 9, 21, 14]
minimum = temps[0]          # initialise to first element (18)
for i in range(1, len(temps)):
    if temps[i] < minimum:
        minimum = temps[i]
print("Minimum:", minimum)

Trace: minimum starts at 18. i=1: 12<18 → minimum=12. i=2: 25<12? No. i=3: 9<12 → minimum=9. i=4: 21<9? No. i=5: 14<9? No. Output: Minimum: 9.

Find maximum

The find-maximum algorithm is structurally identical to find-minimum — only the comparison operator changes. Instead of checking if the current element is smaller than the running minimum, check if it is larger than the running maximum:

scores = [45, 78, 62, 91, 30, 55]
maximum = scores[0]          # initialise to first element (45)
for i in range(1, len(scores)):
    if scores[i] > maximum:
        maximum = scores[i]
print("Maximum:", maximum)

Trace: maximum starts at 45. i=1: 78>45 → maximum=78. i=2: 62>78? No. i=3: 91>78 → maximum=91. i=4: 30>91? No. i=5: 55>91? No. Output: Maximum: 91.

The only difference from find-minimum is > instead of < in the if condition.

Count occurrences

The count algorithm counts how many elements in an array satisfy a given condition — for example, how many scores are above 50, or how many temperatures are below zero. The steps are:

  1. Initialise a counter variable to 0: count = 0.
  2. Loop from index 0 to the last index (the full array, not starting at 1).
  3. Check the condition for each element. If it is True, add 1 to the counter: count = count + 1.
  4. After the loop, count holds the number of elements that met the condition.
ages = [16, 14, 17, 15, 16, 18, 16, 13]
count = 0
for i in range(len(ages)):
    if ages[i] == 16:
        count = count + 1
print("Count:", count)

Trace: 16==16 → count=1. 14==16? No. 17==16? No. 15==16? No. 16==16 → count=2. 18==16? No. 16==16 → count=3. 13==16? No. Output: Count: 3.

The condition can use any comparison operator: ==, >, <, >=, <=, or !=. For example, counting all values greater than 50 uses if ages[i] > 50.

SQA pseudocode for the three algorithms

In the exam, questions may ask you to write pseudocode rather than Python. The structure maps across directly, with SET … TO replacing assignment, IF … THEN … END IF replacing if, and FOR index FROM … TO … DO … END FOR replacing for … in range(…).

AlgorithmLoop startInitialise toCondition
Find minimumindex 1array[0]array[index] < minimum
Find maximumindex 1array[0]array[index] > maximum
Countindex 00condition appropriate to the problem

Note that find-minimum and find-maximum loop from index 1 (since the initial value is already set to index 0), while the count algorithm loops from index 0 (because the counter starts at 0, not at the first element's value).

Worked examples

Example 1 — Find minimum temperature

An array stores the daily low temperatures (°C) for six days: [18, 12, 25, 9, 21, 14]. Write Python code to find and display the minimum temperature.

1
Initialise: Set minimum = temps[0] — this gives minimum the value 18, which is a real value from the array.
2
Loop from index 1: Use range(1, len(temps)) to visit indices 1 through 5. Index 0 is already handled by the initialisation.
temps = [18, 12, 25, 9, 21, 14]
minimum = temps[0]
for i in range(1, len(temps)):
    if temps[i] < minimum:
        minimum = temps[i]
print("Minimum:", minimum)
3
Trace: minimum=18 → i=1: 12<18 ✓ minimum=12 → i=2: 25<12? No → i=3: 9<12 ✓ minimum=9 → i=4: 21<9? No → i=5: 14<9? No. Output: Minimum: 9. Verified: 9 is the smallest value ✓
Example 2 — Find maximum score

Six exam scores are stored: [45, 78, 62, 91, 30, 55]. Find and display the highest score.

1
Same structure, reversed comparison: Initialise maximum = scores[0] (value 45). Loop from index 1. Change the condition to > instead of <.
2
Python code:
scores = [45, 78, 62, 91, 30, 55]
maximum = scores[0]
for i in range(1, len(scores)):
    if scores[i] > maximum:
        maximum = scores[i]
print("Maximum:", maximum)
3
Trace: maximum=45 → i=1: 78>45 ✓ maximum=78 → i=2: 62>78? No → i=3: 91>78 ✓ maximum=91 → i=4: 30>91? No → i=5: 55>91? No. Output: Maximum: 91. Verified: 91 is the largest value ✓
Example 3 — Count how many match a value

An array stores the ages of pupils who attended an after-school club across 8 sessions: [16, 14, 17, 15, 16, 18, 16, 13]. Count how many sessions were attended by a pupil aged exactly 16.

1
Initialise counter: count = 0. The counter starts at zero — no sessions have been counted yet.
2
Loop from index 0: Unlike find-min/max, count always starts the loop at 0 because it does not use the first element as an initial value.
ages = [16, 14, 17, 15, 16, 18, 16, 13]
count = 0
for i in range(len(ages)):
    if ages[i] == 16:
        count = count + 1
print("Count:", count)
3
Trace: 16==16 → count=1. 14==16? No. 17==16? No. 15==16? No. 16==16 → count=2. 18==16? No. 16==16 → count=3. 13==16? No. Output: Count: 3. Verified: there are three 16s in the array ✓
Example 4 — SQA pseudocode for find minimum

Write SQA pseudocode to find the minimum value in an array of five prices: [4.50, 2.99, 6.75, 3.20, 5.00].

1
Map Python constructs to pseudocode: Use SET … TO for assignment, IF … THEN … END IF for conditions, FOR index FROM … TO … DO … END FOR for loops. The upper bound is LEN(prices) - 1 (= 4 for a 5-element array).
2
Complete pseudocode:
SET prices TO [4.50, 2.99, 6.75, 3.20, 5.00]
SET minimum TO prices[0]
FOR index FROM 1 TO LEN(prices) - 1 DO
    IF prices[index] < minimum THEN
        SET minimum TO prices[index]
    END IF
END FOR
SEND "Minimum price: " + minimum TO DISPLAY
3
Verify: minimum=4.50. index=1: 2.99<4.50 ✓ minimum=2.99. index=2: 6.75<2.99? No. index=3: 3.20<2.99? No. index=4: 5.00<2.99? No. Output: Minimum price: 2.99. Verified: 2.99 is the smallest price ✓
Now you try

An array stores the rainfall (mm) recorded each day for a week: rainfall = [23, 45, 12, 67, 34, 19, 8].

Answer the following:

  1. Write Python code to find and display the maximum rainfall recorded in the week.
  2. Write Python code to count how many days had rainfall of 20 mm or less.
  3. By tracing the find-minimum algorithm, what is the minimum rainfall recorded? Show your working.
  1. rainfall = [23, 45, 12, 67, 34, 19, 8]
    maximum = rainfall[0]
    for i in range(1, len(rainfall)):
        if rainfall[i] > maximum:
            maximum = rainfall[i]
    print("Maximum rainfall:", maximum)
    Trace: maximum=23 → i=1: 45>23 ✓ max=45 → i=2: 12>45? No → i=3: 67>45 ✓ max=67 → i=4: 34>67? No → i=5: 19>67? No → i=6: 8>67? No. Output: Maximum rainfall: 67
  2. rainfall = [23, 45, 12, 67, 34, 19, 8]
    count = 0
    for i in range(len(rainfall)):
        if rainfall[i] <= 20:
            count = count + 1
    print("Days with 20mm or less:", count)
    Trace: 23≤20? No. 45≤20? No. 12≤20? Yes (count=1). 67≤20? No. 34≤20? No. 19≤20? Yes (count=2). 8≤20? Yes (count=3). Output: Days with 20mm or less: 3
  3. minimum=rainfall[0]=23. i=1: 45<23? No. i=2: 12<23 ✓ minimum=12. i=3: 67<12? No. i=4: 34<12? No. i=5: 19<12? No. i=6: 8<12 ✓ minimum=8. Minimum rainfall: 8 mm
Common mistakes
Initialising minimum (or maximum) to 0. Setting minimum = 0 seems reasonable, but it breaks the algorithm the moment any array value is positive. For example, if the array is [5, 3, 8], no element is less than 0, so minimum stays at 0 — a value that is not even in the data. Always initialise to array[0], which guarantees the starting value is a real entry from the dataset.
Starting the count loop at index 1. The count algorithm must start at index 0 (unlike min/max which start at 1). Writing for i in range(1, len(data)): skips the first element, so it will never be counted even if it satisfies the condition. Use range(len(data)) for count — no starting offset needed because the counter is initialised to 0, not to data[0].
Using = instead of == in the count condition. Writing if data[i] = 16: causes a SyntaxError — a single equals sign is assignment in Python, not a comparison. The equality comparison operator is always == (double equals). This applies inside both if statements and loops.
Initialising count to 1 instead of 0. If count = 1 before the loop, every final answer will be one too high. A counter starts at zero because no elements have been examined yet — the loop adds 1 each time it finds a match, so the final result equals the number of matches found.
Using the wrong comparison in pseudocode loop bounds. The find-minimum and find-maximum pseudocode loop uses FOR index FROM 1 TO LEN(array) - 1 DO. Writing TO LEN(array) (without the minus 1) attempts to access an index that does not exist and would cause an out-of-bounds error. The minus 1 is always required when the loop upper bound is expressed using LEN().
Exam tip

The SQA regularly tests these algorithms using trace tables and "write the code" questions. In a trace question, work through the loop iteration by iteration and write down the value of the key variable (minimum, maximum, or count) after each step — do not try to work it out in your head. Examiners award marks for the trace, not just the final answer.

When asked to write pseudocode for find-minimum or find-maximum, the two most commonly penalised errors are: (a) initialising to 0 instead of array[0], and (b) writing TO LEN(array) instead of TO LEN(array) - 1 in the loop bounds. Both cost marks even if the rest of the answer is correct.

Also watch for the command word "describe" — it asks for a step-by-step description of how the algorithm works, not just the code. A good description names the initialisation, the comparison, and the update step: "Set minimum to the first element. Loop through the remaining elements. If the current element is less than minimum, update minimum to that element's value."

Task Set

Questions 1–5 are auto-checked. Questions 6–10 are self-marked — write your answer, then reveal the model answer to check your work.

1. What value should the variable minimum be initialised to at the start of a find-minimum algorithm? TYPE 1

2. An array is declared as nums = [7, 3, 9, 1, 5]. After a find-minimum algorithm runs, what is the value of minimum? TYPE 1

3. A count algorithm is used on an array. What value must count be set to before the loop begins? TYPE 1

4. An array is declared as values = [10, 25, 8, 17, 32, 4]. What is the maximum value? TYPE 1

5. The following code runs on data = [5, 12, 3, 8, 12, 7, 12]. What does it output?

count = 0
for i in range(len(data)):
    if data[i] == 12:
        count = count + 1
print(count)
TYPE 1

6. An array temps = [15, 8, 22, 5, 19, 11] stores temperatures in °C. Write Python code to find and display the minimum temperature. Include a trace of the minimum variable at each step. TYPE 2

temps = [15, 8, 22, 5, 19, 11]
minimum = temps[0]
for i in range(1, len(temps)):
    if temps[i] < minimum:
        minimum = temps[i]
print("Minimum temperature:", minimum)

Trace: minimum=15. i=1: 8<15 ✓ minimum=8. i=2: 22<8? No. i=3: 5<8 ✓ minimum=5. i=4: 19<5? No. i=5: 11<5? No. Output: Minimum temperature: 5. Verified: 5 is the smallest value in [15, 8, 22, 5, 19, 11] ✓

7. Write SQA pseudocode for a program that finds the maximum value in an array called prices containing the values [12.99, 8.50, 24.99, 6.75, 15.00]. Display the result. TYPE 2

SET prices TO [12.99, 8.50, 24.99, 6.75, 15.00]
SET maximum TO prices[0]
FOR index FROM 1 TO LEN(prices) - 1 DO
    IF prices[index] > maximum THEN
        SET maximum TO prices[index]
    END IF
END FOR
SEND "Maximum price: " + maximum TO DISPLAY

Trace: maximum=12.99. index=1: 8.50>12.99? No. index=2: 24.99>12.99 ✓ maximum=24.99. index=3: 6.75>24.99? No. index=4: 15.00>24.99? No. Result: 24.99 ✓. Note the loop bound is LEN(prices) - 1 = 4, so the loop visits indices 1, 2, 3, 4.

8. The code below is meant to count how many values in the array are greater than 50, but it has an error. Identify the error, explain what happens when the program runs, and write the corrected code.

marks = [45, 72, 38, 88, 61, 55]
count = 1
for i in range(len(marks)):
    if marks[i] > 50:
        count = count + 1
print("Count:", count)
TYPE 2

Error: count = 1 — the counter is initialised to 1 instead of 0. This means the final count will be 1 too high, producing the wrong answer without any error message from Python.

Effect: Values >50 in the array are 72, 88, 61, 55 — four values. The loop adds 1 four times, but because count started at 1, the output is Count: 5 instead of the correct Count: 4.

Corrected code:

marks = [45, 72, 38, 88, 61, 55]
count = 0
for i in range(len(marks)):
    if marks[i] > 50:
        count = count + 1
print("Count:", count)

Trace: 45>50? No. 72>50? Yes (count=1). 38>50? No. 88>50? Yes (count=2). 61>50? Yes (count=3). 55>50? Yes (count=4). Output: Count: 4

9. Write a complete Python program that stores 6 pupil scores in an array: [67, 42, 88, 55, 73, 90]. The program should display: (a) the highest score, (b) the lowest score, and (c) a count of how many scores are 60 or above (a pass). TYPE 3

scores = [67, 42, 88, 55, 73, 90]
maximum = scores[0]
minimum = scores[0]
count = 0
for i in range(len(scores)):
    if scores[i] > maximum:
        maximum = scores[i]
    if scores[i] < minimum:
        minimum = scores[i]
    if scores[i] >= 60:
        count = count + 1
print("Highest score:", maximum)
print("Lowest score:", minimum)
print("Passes (60 or above):", count)

Trace (i=0 to 5):

i=0: 67>67? No. 67<67? No. 67≥60? Yes (count=1).
i=1: 42>67? No. 42<67 ✓ min=42. 42≥60? No.
i=2: 88>67 ✓ max=88. 88<42? No. 88≥60? Yes (count=2).
i=3: 55>88? No. 55<42? No. 55≥60? No.
i=4: 73>88? No. 73<42? No. 73≥60? Yes (count=3).
i=5: 90>88 ✓ max=90. 90<42? No. 90≥60? Yes (count=4).

Output: Highest=90 ✓, Lowest=42 ✓, Passes=4 ✓. All three algorithms run in a single loop — this is efficient but not required; separate loops are also acceptable.

10. The pseudocode below is meant to find the minimum value in an array but contains two errors. Identify both errors, explain what goes wrong, and write corrected pseudocode.

SET numbers TO [34, 12, 56, 8, 29]
SET minimum TO 0
FOR index FROM 0 TO LEN(numbers) DO
    IF numbers[index] < minimum THEN
        SET minimum TO numbers[index]
    END IF
END FOR
SEND "Minimum: " + minimum TO DISPLAY
TYPE 3

Error 1: SET minimum TO 0. Initialising to 0 means the algorithm looks for a value less than 0. Every value in the array (34, 12, 56, 8, 29) is greater than 0, so the condition numbers[index] < minimum is never True. The minimum stays at 0, which is not in the data and is completely wrong.

Error 2: FOR index FROM 0 TO LEN(numbers) DO. LEN(numbers) = 5, so on the last iteration the loop tries to access numbers[5]. The array only has indices 0–4, so this is an out-of-bounds error. The upper bound must be LEN(numbers) - 1. Additionally, since minimum is initialised to numbers[0] in the corrected version, the loop should start from index 1, not 0.

Corrected pseudocode:

SET numbers TO [34, 12, 56, 8, 29]
SET minimum TO numbers[0]
FOR index FROM 1 TO LEN(numbers) - 1 DO
    IF numbers[index] < minimum THEN
        SET minimum TO numbers[index]
    END IF
END FOR
SEND "Minimum: " + minimum TO DISPLAY

Trace: minimum=34. index=1: 12<34 ✓ minimum=12. index=2: 56<12? No. index=3: 8<12 ✓ minimum=8. index=4: 29<8? No. Output: Minimum: 8. Verified: 8 is the smallest value in [34, 12, 56, 8, 29] ✓

Teacher notes — Shift+T to hide

Suggested timing: 60 minutes. Warm up 8 min; notes 15 min; worked examples 15 min; now you try 7 min; task set 15 min.

Key misconception to address: Initialising minimum or maximum to 0. This is by far the most common error. Use a concrete counter-example: ask pupils to find the minimum of [5, 8, 3] using minimum = 0 — walk through the trace and show that no update ever fires, so minimum wrongly stays at 0. Then show the correct version with minimum = array[0] and confirm it works.

Live demo suggestion: Open a Python REPL. Build the find-minimum algorithm incrementally: first show the array and the initialisation, then add the loop step by step, adding print statements to show minimum changing at each iteration. Run the buggy version (count=1) from Q8 first so pupils can see the wrong output — then fix it live and show the correct result.

Comparing the three algorithms: Draw a quick summary table on the board: min (initialise to array[0], loop from 1, condition <), max (same, condition >), count (initialise to 0, loop from 0, condition = problem-specific). Pupils tend to remember these as a family once they see the pattern side by side.

Extension question: Ask pupils to combine all three into a single program that reads 5 integers from the keyboard, stores them in an array, then prints the minimum, maximum, and count of values above the average. This integrates SDD11 (round), SDD9 (fixed loops), SDD12 (arrays), and SDD14 into one problem — good preparation for the assignment.

SQA command words covered: "trace" (Q2, Q4, Q5, Q6), "write" (Q6, Q7, Q9), "identify" and "explain" (Q8, Q10), "describe" (implicit in pseudocode questions — see exam tip).