Algorithm Specification

You learned three standard algorithms for National 5:

For Higher, we add three more standard algorithms:

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:

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:

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:


Example

Write a program which:

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.

Find Min

Pseudocode

Python

Find Max

Pseudocode

Python