Algorithm Specification
You learned three standard algorithms for National 5:
Input validation
Running total in a loop
Traversing an array
For Higher, we add three more standard algorithms:
Linear search
Count occurrences
Find minimum/maximum
All three of these algorithms are based on traversing an array.
Traversing an array in Python
for counter in range(0, 6):
print(names[counter])
The linear search and count occurrences algorithms use this existing algorithm, and add an if statement inside the loop.
Linear Search
The linear search algorithm searches an array for a value.
It will return:
the position of the search item in the array
or a message to indicate the search item was not found
If an item appears in an array multiple times, the algorithm can be used to search through the entire array from start to finish and identify all of the locations where the item was found.
Example
Write a program which:
asks the user to input a name
displays the position of the name in a list of names
or displays a message to say that the name was not found
Pseudocode
Python
Count Occurrence
The count occurrences algorithm loops through an array counting the number of instances of a given value.
It could be used to check for an exact match - for example, in a list of names, how many people are called Bob?
However, we could also use it with other conditions:
In a list of percentages, how many are above 75%?
In a list of integers, how many are even?
In a list of letters, how many are upper-case?
Example
Write a program which:
asks the user to input an airport code
counts the number of instances of that code in an array of airport codes
displays the result
Pseudocode
Python
Find Min/Max
This algorithm will find the smallest or largest value in an array or array of records.
It works by using the first value in the array as the smallest or largest value.
It then compares the first value with the next value in the array and decides which one is smaller or larger.
This process continues until the entire array has been traversed and every value has been checked.