Find the minimum

From Higher Computing Science
Jump to: navigation, search


Key points

  • Find the minimum is an algorithm that finds the smallest number in array. To do this, a variable will be used to store the smallest number. Each item in the array will be compared to this number, and if the number in the array is smaller than the lowest, then the variable that contains the lowest will be set to this new number.

Information

Haggis implementation

# Algorithm with position
 SET list TO [1,4,2,5,3]
 SET min TO list[0]
 SET minpos TO 0
 FOR counter FROM 0 TO 4 DO
   IF list[counter]<min THEN
     SET min TO list[counter]
     SET minpos TO counter
  END IF
 END FOR
 SEND "minimum is " & (STRING) min & " found at position " & (STRING) minpos TO DISPLAY

# Algorithm without position
 SET list TO [1,4,2,5,3]
 SET min TO list[0]
 FOR counter FROM 0 TO 4 DO
   IF list[counter]<min THEN
     SET min TO list[counter]
  END IF
 END FOR
 SEND "minimum is " & (STRING) min TO DISPLAY
 


Python implementation

Videos

Further information

Test yourself

Teaching resources