Topic 7 · Algorithm design and problem-solving
Searching, sorting, and the standard methods
Linear search, bubble sort, totalling, counting, and finding the maximum, minimum, and average, step by step.
Linear search
A linear search checks each item in a list in order, from the first to the last, until it finds the target or reaches the end. The list does not need to be sorted.
DECLARE Numbers : ARRAY[1:7] OF INTEGER
DECLARE Index : INTEGER
DECLARE Target : INTEGER
DECLARE Found : BOOLEAN
FOR Index <- 1 TO 7
INPUT Numbers[Index]
NEXT Index
INPUT Target
Found <- FALSE
Index <- 1
WHILE Index <= 7 AND NOT Found DO
IF Numbers[Index] = Target
THEN
Found <- TRUE
ELSE
Index <- Index + 1
ENDIF
ENDWHILE
IF Found
THEN
OUTPUT "Found at position ", Index
ELSE
OUTPUT "Not found"
ENDIFBubble sort
A bubble sort goes through the list comparing each pair of neighbours and swapping them if they are in the wrong order. After each pass, the largest unsorted item has "bubbled" to the end. It stops when a pass makes no swaps.
Press Play and watch the orange pair being compared. Items that are in their final place are filled in.
DECLARE Scores : ARRAY[1:6] OF INTEGER
DECLARE Top : INTEGER
DECLARE Index : INTEGER
DECLARE Temp : INTEGER
DECLARE Swap : BOOLEAN
FOR Index <- 1 TO 6
INPUT Scores[Index]
NEXT Index
Top <- 6
REPEAT
Swap <- FALSE
FOR Index <- 1 TO Top - 1
IF Scores[Index] > Scores[Index + 1]
THEN
Temp <- Scores[Index]
Scores[Index] <- Scores[Index + 1]
Scores[Index + 1] <- Temp
Swap <- TRUE
ENDIF
NEXT Index
Top <- Top - 1
UNTIL NOT Swap OR Top = 1Exam tip. Swapping needs a temporary variable. Without Temp, the first assignment would overwrite one of the values.
Totalling and counting
Totalling keeps a running total: start at 0 and add each value. Counting adds 1 each time something happens. Both variables must be set to 0 before the loop.
DECLARE Mark : INTEGER
DECLARE Total : INTEGER
DECLARE PassCount : INTEGER
DECLARE Student : INTEGER
Total <- 0
PassCount <- 0
FOR Student <- 1 TO 30
INPUT Mark
Total <- Total + Mark
IF Mark >= 50
THEN
PassCount <- PassCount + 1
ENDIF
NEXT Student
OUTPUT "Total: ", Total
OUTPUT "Passed: ", PassCountMaximum, minimum, and average
To find the maximum, keep the largest value seen so far and replace it whenever a bigger value turns up. The minimum works the same way with smaller values. Start both with the first value (or with a value outside the possible range).
The average is the total divided by how many values there are.
DECLARE Temps : ARRAY[1:7] OF REAL
DECLARE Day : INTEGER
DECLARE Highest : REAL
DECLARE Lowest : REAL
DECLARE Total : REAL
FOR Day <- 1 TO 7
INPUT Temps[Day]
NEXT Day
Highest <- Temps[1]
Lowest <- Temps[1]
Total <- 0
FOR Day <- 1 TO 7
IF Temps[Day] > Highest
THEN
Highest <- Temps[Day]
ENDIF
IF Temps[Day] < Lowest
THEN
Lowest <- Temps[Day]
ENDIF
Total <- Total + Temps[Day]
NEXT Day
OUTPUT "Highest: ", Highest
OUTPUT "Lowest: ", Lowest
OUTPUT "Average: ", Total / 7Key terms
- Linear search
- Checking each item in turn until the target is found or the list ends.
- Bubble sort
- Repeatedly comparing and swapping neighbouring items until no swaps are needed.
- Totalling
- Adding each value to a running total.
- Counting
- Adding 1 to a counter each time something happens.
Check yourself
What is the most comparisons a linear search makes on a list of 50 items?Show answer
50, when the item is last or not in the list.
Why does the bubble sort use a Swap flag?Show answer
If a whole pass makes no swaps, the list is already sorted, so the algorithm can stop early.
Why must Total be set to 0 before the loop?Show answer
Otherwise it would start with an unknown value and the total would be wrong.