Find Min - Records

Purpose

The purpose of the Find Minimum algorithm are to look through a list of items and find the minimum value or position. It uses a variable to hold the current minimum value or position.

If a value is found that is lower than the current lowest value or position then that new item is set to the new lowest value or position. This web page will cover them using parallel arrays and also records.

Record Structure 

All record examples will use this record structure:

And we have made an array called pupils which contains 40 of these pupil records .

Find Min Pseudocode (Minimum Value)

lowest = pupils[0].mark

FOR counter FROM 1 to length(pupils)

IF pupils[counter].mark < lowest

Lowest = pupils [counter].mark

END IF

END FOR

You will notice that we iterate from the second record in the array of records (index 1) as we have already used the mark in the record 0 to initialise the Lowest variable. This does make the algorithm very slightly more efficient.

Find Min  (Minimum Value) Records - Python

In this example we have initialised the minimum value as the mark field from the first pupil in the array of records. 

You will notice that we iterate from the second record in the array (index 1) as we have already used the mark in the record 0 to initialise the minimum variable.

We only overwrite this value if we find a new item that is smaller.

Find Min Pseudocode (Position)

Sometimes (particularly when writing functions) it is useful to store the position of the smallest value as opposed to the value itself. That way regardless of if we are using parallel arrays or records we can use it to then retrieve other pieces of data.

You will notice that we iterate from the second record in the array (index 1) as we have already used the mark in the record 0 to initialise the minpos variable.

minpos = 0

FOR counter FROM 1 to length(pupils)

IF pupils[counter].mark < pupils[minpos].mark

minpos = counter

END IF

END FOR

Find Min  (Minimum Position) Records - Python

This time we have set the minimum position as 0 in the variable minpos then only overwrite it with the counter variable when we find a new smaller value

You will notice that we iterate from the second record in the array (index 1) as we have already used the mark in the record 0 to initialise the minpos variable.

This is then returned and can be used outside of the function to find other associated values (in other arrays/records).