To find the maximum of a list, keep one variable holding the largest value seen so far and compare every other item with it. Each time an item is larger, it replaces the stored value.
This lesson uses the loops from tracing count-controlled loops on an array. It sits inside repetition and arrays, and the same pattern gives minimum, count and total.
What are the steps?
- Set
Maxto the first item in the list. - Loop through the remaining items, from position 2 to the last position.
- If the current item is greater than
Max, copy it intoMax. - After the loop,
Maxholds the largest value.
Worked example
An array Scores[1:6] holds 35, 48, 22, 48, 51, 40.
Max ← Scores[1]
FOR i ← 2 TO 6
IF Scores[i] > Max
THEN
Max ← Scores[i]
ENDIF
NEXT i
OUTPUT Max
Max starts as 35.
| i | Scores[i] | Scores[i] > Max? | Max after |
|---|---|---|---|
| 2 | 48 | 48 > 35 true | 48 |
| 3 | 22 | 22 > 48 false | 48 |
| 4 | 48 | 48 > 48 false | 48 |
| 5 | 51 | 51 > 48 true | 51 |
| 6 | 40 | 40 > 51 false | 51 |
The output is 51. Position 4 holds an equal value, 48, and does not replace Max because 48 > 48 is false.
To also report where the maximum is, store the position as well:
Max ← Scores[1]
MaxPos ← 1
FOR i ← 2 TO 6
IF Scores[i] > Max
THEN
Max ← Scores[i]
MaxPos ← i
ENDIF
NEXT i
OUTPUT MaxPos, Max
For the same data, MaxPos changes at i = 2 and i = 5. The output is 5, 51.
The mistake to watch for
A common slip is to start with Max ← 0.
Mistaken algorithm:
Max ← 0, then compare every item including the first.With the temperatures -5, -2 and -9, no value is greater than 0, so the algorithm outputs 0. That value is not in the list.
The correction is Max ← Temps[1], then loop from position 2. Trace it: Max is -5, then -2 > -5 is true so Max becomes -2, then -9 > -2 is false. The output is -2, which is correct.
Check yourself
1. Trace the algorithm on the data 8, 3, 12, 12, 5. What is Max after each item?
Show answer
Start with 8. Then 3 > 8 false (8), 12 > 8 true (12), 12 > 12 false (12), 5 > 12 false (12). The final value is 12.
2. Which single symbol changes to find the minimum of the same data, and what is the result?
Show answer
Change > to <. Start with 8, then 3 < 8 true (3), then 12, 12 and 5 are not less than 3. The minimum is 3.
3. For the data 6, 9, 9, 2 stored in Data[1:4], what position does the position version give, and what would change with >=?
Show answer
With >, 9 at position 2 is stored first, and the second 9 does not replace it, so the position is 2. With >=, the second 9 replaces it, so the position is 3. The value is 9 in both cases.
Where this leads next
Next, look closely at what the loop variable i means compared with the item it points to in using an array index without confusing it with a value. To try your own list, use the Python reasoning sandbox and compare its result with your trace table.
A teacher in online one-to-one Computer Science tuition can give you awkward lists, such as negatives or repeated values, so you learn to predict where an algorithm fails.