LESSON 04 / 12

What Is an Algorithm?

An algorithm is a clear, step-by-step method for solving a problem. Recipes, directions and even tying your shoelaces are algorithms.

๐ŸŽ“Beginnerlevel
โฑ๏ธ15 minto finish
โœ๏ธ6activities

๐Ÿ“‚ Computational Thinking & Pseudocode๐Ÿท๏ธ Computational thinking

๐ŸŽฏ By the end of this lesson you canโ€ฆ

  • Define an algorithm in simple words
  • List the features of a good algorithm
  • Recognise algorithms in everyday life
  • Write a simple algorithm as ordered steps

A method you can follow

An algorithm is a step-by-step method for solving a problem. If youโ€™ve ever followed directions to a friendโ€™s house, youโ€™ve followed an algorithm!

๐Ÿ“˜ New termAlgorithm

A clear, step-by-step method for solving a problem that always finishes.

1BEGIN2    TAKE A, B3    SET C = A + B4    DISPLAY C5END

๐ŸŽ A recipe: the same steps, in the same order, give the same cake every time.

Five circles showing the steps to make tea
Making tea is an algorithm: five clear steps, in order, with a result at the end.

What makes a good algorithm?

  • โœ… Clear โ€” every step means exactly one thing.
  • โœ… Ordered โ€” the steps happen in a definite sequence.
  • โœ… Finite โ€” it always finishes; it doesnโ€™t go on forever.
  • โœ… Correct โ€” it actually solves the problem.
  • โœ… Has inputs and outputs โ€” it takes something in and gives a result.

Program vs algorithm

An algorithm is the idea โ€” the method. A program is that method written for a computer. The same algorithm can be written as pseudocode, PHP or JavaScript โ€” itโ€™s still the same algorithm.

average-of-two.pseudo ๐Ÿ‘† Tap a line to explain it
1BEGIN2    TAKE A, B3    SET SUM = A + B4    SET AVERAGE = SUM / 25    DISPLAY AVERAGE6END
๐Ÿงฎ Trace table โ€” the average algorithm

Run the program in your head, one line at a time. Fill every empty box, then press Check.

1BEGIN2    SET A = 83    SET B = 44    SET SUM = A + B5    SET AVERAGE = SUM / 26    DISPLAY AVERAGE7END

One problem, many algorithms

Say you want to find a word in a dictionary. Algorithm 1: start at page 1 and check every page. Algorithm 2: open near the middle, see if your word is before or after, and keep halving. Both work โ€” but algorithm 2 is much faster. Later in the course youโ€™ll learn to compare algorithms like this.

Check your understanding

๐Ÿ’ก Concept check
Which of these is not a feature of a good algorithm?
๐Ÿ”ฎ What will this display?
What will this algorithm display?
1BEGIN2    SET A = 73    SET B = 54    SET SUM = A + B5    SET AVERAGE = SUM / 26    DISPLAY AVERAGE7END
๐Ÿž Find the error
This algorithm is meant to show the average of A and B, but it shows the wrong answer. Whatโ€™s the mistake?
1BEGIN2    SET A = 103    SET B = 204    SET SUM = A + B5    SET AVERAGE = SUM / 36    DISPLAY AVERAGE7END
๐Ÿ› ๏ธ Your turn: a perimeter algorithm

Write an algorithm that TAKEs the WIDTH and HEIGHT of a rectangle and DISPLAYs its perimeter (all four sides added together).

๐Ÿ—ฃ๏ธ Explain your thinking
Explain why โ€œKeep stirring the soupโ€ is not a good algorithm step.

๐Ÿ“Œ Key takeaways

  • An algorithm is a precise, step-by-step method for solving a problem.
  • Good algorithms are clear, ordered, finite and give the right result.
  • The same problem can have several algorithms โ€” some better than others.
  • A program is an algorithm written so a computer can run it.

Finished reading & practising?

Saved in this browser. Create a free account to keep it forever.