Index
Videos



Overview


We're going to look at the problem of finding a minimum value (min) in an array of numbers.

Let's take a look at the recipe approach (also known as an algorithm).

You'll find that it is quite natural.

Steps


It's always helpful to first solve the problem manually.

This produces the recipe (or fancy: algorithm)
40 20 10 30
  • Keep track of a "min" number
  • Walk the list
  • If visited element is smaller, set as min
Generally:
  • We set the initial "min" to the first value.
  • We iterate or walk the list

Each iteration:
If array elem < min
	set min to that elem


In the end we have our min.
Set min to first element = 40
VisitingCondition
(elem<min)
AnswerNew "min"
4040 < 40false40
2020 < 40true20
1010 < 20true10
3030 < 10false10
Pseudocode follows nicely from the recipe.
set var "min" to first list value
iterate the list
	If array elem < min
		set min to that elem
Let us say, the problem also asked us to handle this special case:
//Special Case: if "list" contains no elements, return null

We try to take care of any special case(s) as the first step as shown.
if the list is empty
	return null
set var "min" to first list value
iterate the list
	If array elem < min
		set min to that elem
We would now open our lab, play and solve.

From our recipe, we can see that we have examples that should come in handy:

  • Array Iteration Lab
  • Array Basics Lab
  • Condition Statement Lab
set min to the first value
iterate the array
	If array elem < min
		set min to that elem