On this concept: Formula

Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 3-5

Algorithm

An algorithm is a precise, finite, effective step-by-step procedure that turns valid input into the correct output and then stops — like a recipe or the steps of long division.

output=f(input)

Quick Answer

An algorithm is a precise, finite, effective step-by-step procedure that turns valid input into the correct output and then stops — like a recipe or the steps of long division. You use one whenever a job has to be done the same way every time, by a person or a computer: sorting cards, finding the biggest number in a list, or giving directions. Every step must be exact enough that anyone who follows it gets the same answer, and the steps must come to an end.

Why This Matters

Once you can write an algorithm, you can hand a job to a computer or to another person and trust the result, even for inputs you never tried yourself. Steps like 'add a little' or 'keep going until it looks right' fail, because a computer cannot guess what you meant, and steps that never end never give an answer at all.

Intuitive Explanation

Imagine telling a robot how to find the tallest student in a line. 'Look and pick the tallest' does not work, because the robot does not know how to look and pick. Instead you say: point at the first student and remember their height. Move to the next student. If they are taller than the height you remember, remember their height instead. Repeat until there are no students left. Then say the height you remember. Every step is exact, every step is something the robot can do, and the steps stop at the end of the line. That is an algorithm.

Try it on heights of 130, 142, 128, 150, and 139 cm. Remember 130. 142 is taller, so remember 142. 128 is not taller. 150 is taller, so remember 150. 139 is not taller. The line has ended, so the answer is 150 cm. The very same steps work for a line of 5 students or 500.

Not every list of steps is an algorithm. A cookie recipe that says 'add sugar to taste' or 'bake until golden' leaves the choice to the cook, so two people get different cookies. A step like 'stir forever' never ends. An algorithm replaces 'to taste' with '100 grams' and 'until golden' with '12 minutes', or with a test that anyone can check the same way.

Think of it like this. An algorithm is like a recipe written for a robot cook: every amount is measured, every step is spelled out, and the last step says stop. A person can guess what 'a pinch' means; the robot cannot, so nothing is left to guess. Unlike most recipes, a good algorithm must also work for every input it is allowed to get, not just one.

Core idea. An algorithm must be precise, finite, and guaranteed to produce a result for valid inputs.


When to Use

Use an algorithm when a task needs a step-by-step method that works every time, for any input: sorting, searching, counting, adding big numbers, finding a route. Ask for exact steps and a clear stop. If the task is only to split a big job into smaller jobs, that is Decomposition. If it is only about doing steps in the right order, that is Sequence; if it is only about repeating a step, that is Iteration.

Tip. Ask: Does the task need a complete list of exact steps that always ends with the right answer for any allowed input?

How to Recognize It

Before calling something an algorithm, look at the steps themselves: are they all there, exact, and sure to finish?

  1. Could someone who has never done this job follow every step without guessing?

    Yes means the steps are exact enough. No, because a step says something like 'add some' or 'make it nice', means you have a rough plan, not an algorithm yet.

  2. Do the steps cover the whole job, from the starting information to the finished answer?

    Yes points to an algorithm. If you have only split the job into smaller jobs and not written their steps, that is Decomposition.

  3. Will the steps always come to an end?

    Yes is needed for an algorithm. If one step can send you back to the start forever with no way out, the steps never finish and never give an answer.

  4. Is the question only about which step comes first, second and third?

    Yes points to Sequence, which is one part of an algorithm. An algorithm can also choose between paths and repeat steps, as long as it ends with the right answer.

Algorithm vs Decomposition vs Sequence vs Flowchart

All four come up when you plan how to get a job done, so they blur together. Ask whether you have the full set of exact steps that finishes with the answer, or only one piece of it.

Algorithm

Meaning
An algorithm is the whole set of exact, doable steps that takes the starting information to the right answer and then stops.
Key test
Every step is exact, the whole job is covered, and it always stops.
Formula
output=f(input)
Example
To find the biggest number in a list: remember the first number, compare each next one, keep the bigger, stop at the end.

Decomposition

Meaning
Decomposition splits a big job into smaller jobs, before anyone writes the exact steps for each one.
Key test
You get a list of smaller jobs, not the steps to do them.
Example
Planning a class play as costumes, lines, music and the stage, each handled by its own group.

Sequence

Meaning
Sequence is the order the steps run in, one after another, which is one part of every algorithm.
Key test
Only the order of the steps matters, with no choosing or repeating.
Example
Put on socks, then shoes: swap the order and the job goes wrong.

Flowchart

Meaning
A flowchart is a drawing of an algorithm, with boxes for steps, diamonds for questions and arrows showing the order.
Key test
It is a way to show an algorithm, not a different kind of solution.
Example
A chart that starts at an oval, asks 'Is it raining?' in a diamond, and points to 'Take an umbrella'.

Formula & Notation

output=f(input)
An algorithm is a finite sequence of well-defined instructions that, given valid input, produces the correct output and terminates in a finite number of steps.

Worked Examples

Example 1 — Trace the digit-adding algorithm

Easy

Here is an algorithm. Step 1: add up all the digits of the number. Step 2: if the answer has more than one digit, go back to step 1 using the answer. Step 3: otherwise, say the answer. Run it on 4,782.
Show the steps and the answer
  1. Step 1: 4+7+8+2=21.

    Each digit of 4,782 is added exactly once.

  2. Step 2: 21 has two digits, so go back to step 1 with 21.

    The rule says to repeat whenever the answer is longer than one digit.

  3. Step 1 again: 2+1=3.

    This time the number being added up is 21, not 4,782.

  4. Step 2: 3 has only one digit, so step 3 says the answer, 3.

    Adding the digits of a number with two or more digits always gives a smaller number, so the algorithm is sure to reach one digit and stop.

Answer: 3

Takeaway: Follow each step exactly as written, and notice what makes the algorithm reach its stop.

Example 2 — Clear steps that never stop

Standard

Mia writes these steps. Step 1: start with the number 1. Step 2: add 2. Step 3: if the number is 10, stop; otherwise go back to step 2. Does this always finish? Trace it.
Show the steps and the answer
  1. Trace: 1, 3, 5, 7, 9, 11, 13, and so on.

    Adding 2 to an odd number always gives another odd number.

  2. Check the stop rule: the number must be exactly 10, but it is always odd, so it is never 10.

    The number jumps from 9 straight to 11, so the test in step 3 is never true.

  3. Conclude: the steps never end, so they are not an algorithm.

    An algorithm must stop after a limited number of steps, however clear each step is.

  4. Fix the stop rule: 'If the number is 10 or more, stop.' Now it stops at 11, after 5 additions.

    '10 or more' becomes true as soon as the number passes 10.

Answer: No: the numbers 1, 3, 5, 7, 9, 11, ... are always odd, so they never equal 10. With the rule 'stop at 10 or more', it stops at 11.

Takeaway: Clear steps are not enough; check that the stop rule will really come true.

Example 3 — Find the mistake: it passed two tests

Application

Sam writes an algorithm to tell whether a whole number is even: "Look at the last digit. If it is 2, 4, 6, or 8, say even. Otherwise, say odd." He tests 14 and 37, gets both right, and says it works. Find the mistake and correct it.
Show the steps and the answer
  1. Test a different kind of input: a number that ends in 0, such as 30.

    An algorithm has to work for every whole number, not just the two Sam tried.

  2. Trace: the last digit of 30 is 0, which is not 2, 4, 6, or 8, so the steps say 'odd'.

    But 30=2×15, so 30 is even, and the algorithm gives a wrong answer.

  3. Fix the list of digits to 0, 2, 4, 6, 8.

    A whole number is even exactly when its last digit is one of these five.

  4. Check the fixed version: 30 gives even, 14 gives even, 37 gives odd.

    It still gets Sam's two tests right and now handles numbers ending in 0.

Answer: Corrected algorithm: if the last digit is 0, 2, 4, 6, or 8, say even; otherwise say odd. It now says 30 is even.

Takeaway: Passing a couple of tests does not prove an algorithm works; try unusual inputs too, like numbers that end in 0.

Where You'll See It

  • Making instant noodles from the back of the package

    The numbered steps (boil 2 cups water, add noodles, wait 3 minutes, stir in seasoning) are an algorithm: precise, finite, and each step is something you can actually do.

  • Tying your shoelaces the same way every morning

    You follow the exact same ordered moves every time and always get a tied knot, which is what makes it an algorithm rather than random fiddling.

  • GPS finding the fastest route to soccer practice

    The app runs a route-finding algorithm that takes your start and destination as input and outputs a turn-by-turn path, the same way for any two points you pick.

Common Mistakes

  1. The slip: An algorithm is just any list of steps, even if a step is vague like 'add a little salt' or 'make it look nice.'

    Why it's tempting: Recipes for people use words like 'a little' or 'to taste' and humans fill in the gaps, so it feels like that counts as an algorithm.

    Instead: Every step must be unambiguous and effective: a computer cannot judge 'a little,' so an algorithm needs a precise amount or rule a machine can carry out.

  2. The slip: If my steps work on one example, the algorithm is finished and correct.

    Why it's tempting: Seeing the right answer on your test case feels like proof it works.

    Instead: An algorithm must produce a correct result for all valid inputs, including edge cases like empty input or the largest/smallest values, not just one example.

  3. The slip: An algorithm can run forever as long as it keeps doing useful work.

    Why it's tempting: Many real programs (like a game) seem to 'run forever,' so termination feels optional.

    Instead: An algorithm must be finite: it has to terminate after a bounded number of steps and stop with an answer for valid input.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. Follow these steps with the number 6. Step 1: double it. Step 2: add 3. Step 3: write the answer. What do you write?

    Hint: Do the steps in order, one at a time.

  2. A robot starts on square 1 of a number track. Its steps are: "Move forward 2 squares. Do this 3 times." Which square does it end on?

    Hint: Work out how far it moves in total, then count on from square 1.

  3. Steps to find the biggest number in a list: call the first number "biggest". For each next number, if it is bigger than "biggest", it becomes the new "biggest". Trace the steps on 4, 9, 2, 7.

    Hint: Write down what "biggest" is after you look at each number.

  4. An algorithm counts even numbers: look at each number, and if its last digit is 0, 2, 4, 6 or 8, add 1 to the count. Run it on 12, 7, 30, 15, 8. What is the count?

    Hint: Check the last digit of each number, one by one.

  5. You are asked to list the big jobs for a class party: food, games and decorations. Is that an algorithm, or decomposition?

    Hint: Does the list give exact steps in order that finish with a result?

  6. A recipe step says: "Bake until it looks done." Why is this not a good algorithm step? Rewrite it.

    Hint: Could two different people follow this step and do the same thing?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Is a computer program the same thing as an algorithm?

Not quite. An algorithm is the plan of steps, and a program is that plan written in a language a computer understands, such as Scratch or Python. One algorithm can be written as many different programs. The steps for finding the biggest number in a list stay the same whether you write them in English, draw them as a flowchart, or type them as code.

Can two different algorithms solve the same problem?

Yes, and often one is much quicker. To find a word in a dictionary, you could read every page from the start, or you could open the book near the right letter and keep narrowing down. Both always find the word, so both are algorithms, but the second takes far fewer steps. Choosing between algorithms that both work is a big part of computer science.

Does a game that runs until I close it break the rule that algorithms must stop?

No. A game is a program built from many smaller algorithms, and each of those does finish: one draws the next picture, one checks whether you scored, one moves the characters a little. The game runs these small algorithms again and again until you quit. The rule about stopping applies to each job the program does, not to how long you keep playing.

Who invented algorithms?

People used algorithms long before computers. Around 300 BC the Greek mathematician Euclid wrote down exact steps for finding the largest number that divides two numbers evenly, and people still use them today. The word 'algorithm' comes from al-Khwarizmi, a Persian scholar who lived about 1,200 years ago and wrote about step-by-step methods for doing arithmetic with written numbers.

Learning Path

← Before

No prerequisites
Algorithm

You are here

Nothing comes before this lesson; it is where computational thinking starts. An algorithm is the idea that a job can be written as exact steps that finish with the right answer. Next, Decomposition shows how to break a big job into smaller ones before writing steps, Sequence explains why the order of the steps matters, and Iteration shows how to repeat a step without writing it many times.

See Also