Skip to main content

Computational thinking

Algorithm Design

Describe a solution in clear, ordered steps that a person or a computer can follow.

Try the example
Four towers of 3, 6, 4 and 2 blocks. A dark marker sits in front of the tallest tower.

Find the largest number

The towers are 3, 6, 4 and 2 blocks tall. Read them from left to right, remembering the largest number seen so far.

Step 1 / 4

Start with the first number

The first number is 3, so 3 is the largest so far. Starting with an actual list value also works when all the numbers are negative.

What makes an algorithm reliable?

  1. Give each step one meaning

    “Compare the next number with the stored number” gives a precise action. State the input requirement too. This example needs at least one number.

  2. Put the steps in order

    Store the first number before comparing anything with it. Each step needs its inputs to be ready when it begins.

  3. Choose and repeat

    A condition decides whether to update the stored number. A loop repeats the comparison for each remaining item.

  4. Stop and return the result

    Move to the next value on every pass. When none remain, stop and return the stored value.

Four ways to express the same algorithm

Everyday language

Say, “Remember the first number. At each new number keep the larger of the two. At the end, say the number you kept.”

A flowchart

Connect actions with arrows. At “Is the next number larger?” one branch updates the stored value. Both branches lead back to checking for more numbers.

Pseudocode

Give the instructions a structure. Initialise the value, repeat the comparison, then return the result. Choose words that make the logic easy to follow.

Working code

Here is the same algorithm in Python. C++ or JavaScript can express the same sequence of decisions.

Python
def find_largest(numbers):
    largest = numbers[0]
    for number in numbers[1:]:
        if number > largest:
            largest = number
    return largest

How the skills connect

We isolated one job, spotted the repeated comparison and represented each tower by its height. Writing those steps gave us an algorithm we can generalize to any non-empty list of numbers.