Algorithm definition
An algorithm is a precise sequence of steps for solving a problem or producing a result.
Algorithm in a picture
How it works
An algorithm specifies its input, the actions it takes, and what it returns or when it stops. A correct algorithm gives the intended result for every valid input. You can write its steps in words, pseudocode, or a programming language.
Worked examples
Give an algorithm for finding the total of a list.
Start a total at 0. Add each list item to it, one at a time, then return the total.
Can two algorithms solve the same problem?
Yes. They may use different steps or require different amounts of work.
Why specify the valid input?
It tells you when the procedure is expected to produce the promised result.
Common mistake
State a stopping condition. Steps that keep repeating forever do not finish the computation for that input.
Quick check
Can an algorithm be written without code?
Show answer
Yes, if the steps are precise enough.
Is a worked answer by itself a general algorithm?
Show answer
No. A general algorithm gives steps that apply to its valid inputs.
Sources
Flashcard
Algorithm
Reveal the definition
An algorithm is a precise sequence of steps for solving a problem or producing a result.
Effortless Math. “Algorithm.” Math Dictionary.
