Mathematics I / Algorithms and Computational Thinking
Practice question · Put in order

Order the steps of the algorithm that finds the largest value in a list.

Hints
  1. An algorithm must start from a definite state before it can compare anything.
  2. The comparison happens after you have an element in hand, and the report happens only once the list is exhausted.
Show the answer
  1. Set best equal to the first element of the list
  2. Look at the next element in the list
  3. If that element is greater than best, set best to it
  4. Repeat until no elements remain
  5. Report best as the maximum
Why

You must initialise before you can compare, compare before you can update, and exhaust the list before you can be sure. Starting best at zero instead of the first element is the usual bug, it fails on a list of negative numbers.

Read the lesson: Algorithms and Computational Thinking →

Practise Algorithms and Computational Thinking

The app has 6 more questions on this lesson, and keeps your place in the course. Mathematics I is free to start.

More questions on Algorithms and Computational Thinking