SDD14 — Min, Max, Count Occurrences
- I can write a standard algorithm to find the minimum value in a 1D array
- I can write a standard algorithm to find the maximum value in a 1D array
- I can write a standard algorithm to count how many times a target value occurs in a 1D array
- I can implement a minimum-finding algorithm using a running "smallest so far" variable
- I can implement a maximum-finding algorithm using a running "largest so far" variable
- I can implement a count-occurrences algorithm using a running counter, checking every element
- I can explain why all three algorithms check every element in the array, unlike linear search
stockLevels = [30, 45, 20, 15, 60, 18], what index does a linear search for 15 return?Key vocabulary
for loop, to process each one.Finding the minimum, maximum, and counting occurrences
Three algorithms, one shared shape
SDD13's linear search stopped scanning the moment it found a match. The three standard algorithms in this lesson — finding the minimum, finding the maximum, and counting occurrences — all work differently: each one must examine every single element of the array before it can give a final answer, since there is no way to know the smallest, largest, or total count of matches without having looked at everything. This means all three are naturally written with a for loop that always runs to completion, rather than linear search's early-exit while loop.
Finding the minimum
To find the minimum, a variable (commonly called smallest) is first set to the array's very first element. The algorithm then compares every remaining element to smallest: whenever an element is found that is less than the current smallest, smallest is updated to that new, lower value. By the time every element has been checked, smallest holds the true minimum of the whole array.
Finding the maximum
Finding the maximum works identically, but in reverse: a variable (commonly largest) starts at the first element, and is updated whenever a greater element is found. The only difference between the minimum and maximum algorithms is the comparison operator used (< versus >) — the overall structure, and the reason it works, is exactly the same.
Counting occurrences
Counting occurrences answers a different question: not "what is the smallest/largest value", but "how many times does this specific target value appear?" A counter variable starts at 0, and every element of the array is compared to the target; each time a match is found, the counter is increased by 1. Unlike minimum/maximum, which track a value seen so far, counting occurrences tracks a running total of matches — but the underlying shape (a for loop that visits every element exactly once) is the same.
Why starting at the first element matters
Both the minimum and maximum algorithms start their running value at array[0], then loop through the remaining elements (from index 1 onwards) comparing against it. Starting that loop from index 0 after this initialisation is redundant but not incorrect. The unsafe choice is an arbitrary starting value such as 0: smallest = 0 gives the wrong result when the true minimum is positive, because no element is less than 0, although it may accidentally work when negative values are present. Symmetrically, largest = 0 gives the wrong result when the true maximum is negative, although it may accidentally work when positive values are present. Initialising from array[0] is data-independent and guarantees the result is genuinely present in the array.
if array[position] < smallest:
smallest = array[position]if array[position] > largest:
largest = array[position]if array[position] == target:
count = count + 1Worked examples
stockLevels array from SDD13:
stockLevels = [30, 45, 20, 15, 60, 18]
def findMinimum(array):
smallest = array[0]
for position in range(1, len(array)):
if array[position] < smallest:
smallest = array[position]
return smallest
result = findMinimum(stockLevels)smallest starts at 30 (index 0). Comparing each remaining element: 45 (not smaller), 20 (smaller — smallest becomes 20), 15 (smaller — smallest becomes 15), 60 (not smaller), 18 (not smaller than 15).result is 15 — confirmed by an actual Python run. Every one of the 6 elements was checked, even though the true minimum was found partway through.def findMaximum(array):
largest = array[0]
for position in range(1, len(array)):
if array[position] > largest:
largest = array[position]
return largest
result = findMaximum(stockLevels)> instead of < — and largest replaces smallest as the running value.result is 60 — confirmed by an actual Python run.dailyCustomers = [42, 38, 42, 55, 42, 30]. Counting how many days had exactly 42 customers:
def countOccurrences(array, target):
count = 0
for position in range(len(array)):
if array[position] == target:
count = count + 1
return count
result = countOccurrences(dailyCustomers, 42)42: index 0 (match, count → 1), index 1 (no match), index 2 (match, count → 2), index 3 (no match), index 4 (match, count → 3), index 5 (no match).result is 3 — confirmed by an actual Python run. Note the loop runs across the whole array (range(len(array)), starting at index 0), unlike minimum/maximum which start comparing from index 1.findMaximum(dailyCustomers) on [42, 38, 42, 55, 42, 30], recording position, array[position], and largest after each comparison:| position | array[position] | largest (after) |
|---|---|---|
| 0 (start) | 42 | 42 |
| 1 | 38 | 42 |
| 2 | 42 | 42 |
| 3 | 55 | 55 |
| 4 | 42 | 55 |
| 5 | 30 | 55 |
55, updated only once (at index 3), even though the array is checked in full.
dailyCustomers = [42, 38, 42, 55, 42, 30], what does countOccurrences(dailyCustomers, 38) return? What does countOccurrences(dailyCustomers, 99) return?
- Initialising
smallest/largestto0instead ofarray[0].smallest = 0fails when the true minimum is positive;largest = 0fails when the true maximum is negative. Either bug can appear to work when values on the other side of 0 overwrite the starting value. Initialising fromarray[0]is safe for either sign. - Starting the minimum/maximum comparison loop from index 0. Since the running value already starts at
array[0], the comparison loop should run from index 1 onward — comparing index 0 to itself is redundant, though not incorrect. - Confusing count-occurrences'
0with linear search's-1. A count of0genuinely means "the target appears zero times" — it is a valid result, not a sentinel signalling an error or absence of the algorithm's ability to answer. - Using a linear-search result as an index before checking its sentinel. Python accepts negative indices, so
array[-1]returns the last element. Always checkif result != -1:before indexing, or a "not found" result silently selects real data. - Using
<when>was intended (or vice versa). Minimum and maximum differ by exactly one comparison operator — swapping them silently finds the wrong extreme. - Stopping early, as if these were a search. Unlike linear search, none of these three algorithms can stop before the end of the array — the true minimum, maximum, or count is not known until every element has been checked.
When writing or explaining minimum/maximum code, always state that the running value is initialised from the array's own first element, not an arbitrary constant like 0 — examiners specifically check for this. When comparing these three algorithms to linear search, be ready to explain why they must check every element (the answer isn't known until the whole array has been seen) while linear search does not (it only needs to confirm one match exists).
Task Set A — Core questions
stockLevels = [30, 45, 20, 15, 60, 18], what is the minimum value?stockLevels array, what is the maximum value?dailyCustomers = [42, 38, 42, 55, 42, 30], how many times does 42 occur?smallest variable be initialised to before a minimum-finding loop begins?dailyCustomers from Question 3, how many times does 99 occur?for loop that runs to completion, rather than an early-exit loop like linear search?countOccurrences that takes an array and a target value as parameters, and returns how many times the target appears in the array.def countOccurrences(array, target):
count = 0
for position in range(len(array)):
if array[position] == target:
count = count + 1
return count
findMinimum initialises smallest to 0, and the array is stockLevels = [30, 45, 20, 15, 60, 18]. What does their code incorrectly return?0 from countOccurrences is a valid, meaningful answer, whereas linear search needs a special sentinel value (-1) to represent "not found". (3 marks)Task Set B — Extension
min(), max(), and list.count() functions. Suggest why this course teaches the manual, hand-written versions of these algorithms rather than just using the built-in functions.Higher Computing Science → Software Design & Development → SDD14
Double period. Three genuinely distinct standard algorithms are covered explicitly and separately here (per the original build-status audit's note that this must not collapse into "one running example with variations") — minimum, maximum, and count-occurrences each get their own worked example and their own dedicated Task Set A question.
Suggested timing: 5 min warm-up + vocab · 25 min notes + algo-grid comparison card · 35 min examples 1–4 including the trace table · 10 min "now you try" · 40 min Task Set A · Task Set B as homework/extension.
Key misconception: pupils very reliably initialise smallest/largest to 0 out of habit (perhaps from other counter variables always starting at 0) — Task Set A Q9 deliberately tests this exact bug against stockLevels (an all-positive array), where it produces a plausible-looking but definitely wrong answer of 0. Note this bug's direction is data-dependent: against an all-negative array, the same incorrect initialisation happens to still produce the correct answer, since every element is already less than 0. Worth demonstrating both cases live if time allows, to show the bug isn't reliably self-revealing.
Deliberately reuses SDD13's stockLevels array for minimum/maximum continuity, but introduces a fresh dailyCustomers array with a genuinely repeated value for the count-occurrences examples, since stockLevels/tuck shop prices have no natural repeats to count.
SQA command words covered: explain, describe, write code, trace/complete a trace table.