What an algorithm is

An algorithm is a finite sequence of precise, unambiguous steps that solves a problem or performs a task. It is the idea behind a program, independent of any programming language: the same algorithm can be written in Python, in C, on paper or as a drawing.

A proper algorithm has five properties:

  • Finite — it always stops after a limited number of steps.
  • Precise (definite) — every step has exactly one meaning; there is nothing to guess.
  • Input — it works on zero or more values given to it from outside.
  • Output — it produces at least one result.
  • Effective — every step is simple enough to actually carry out, by a person with pencil and paper or by a computer.

Why bother writing an algorithm down before you code? Because it separates two hard problems. First you decide what the steps are; only then do you worry about how to write them in a particular language. Mistakes in the logic are much cheaper to find on paper than in a running program.

From an everyday task to precise steps

Suppose you have the maximum temperature of every day last week, and you want the highest one. A person would say: "Look at the numbers and pick the biggest." That works for people, because we can see seven numbers at a glance. A computer cannot "look at the numbers"; it can only compare two values at a time. So the instruction has to be spelled out:

  1. Take the first temperature and remember it as the highest so far.
  2. Go through the other temperatures one by one. For each one: if it is higher than the highest so far, remember it instead.
  3. When there are no temperatures left, the one you remember is the answer.

Each step is now something a computer can do: store a value, compare two numbers, move to the next item. And it is clear when the algorithm stops — when the list runs out. This process of breaking a vague task into small, exact steps is the core skill of programming.

Pseudocode

Pseudocode is a way of writing an algorithm in structured, code-like text that follows no real language's syntax. It is meant for people, not computers: nobody runs it, so you can leave out details such as type declarations or punctuation and focus on the logic. Here is the temperature algorithm:

INPUT temperatures
max ← first item of temperatures
FOR EACH value IN the remaining items of temperatures
    IF value > max THEN
        max ← value
    END IF
END FOR
OUTPUT max

Conventions

There is no single official pseudocode standard; textbooks, exams and companies each use their own style. What matters is that you are consistent. This encyclopedia uses these conventions:

  • Keywords in UPPERCASE: IF … THEN … ELSE … END IF, WHILE … END WHILE, FOR EACH … END FOR, FOR … TO … END FOR, FUNCTION … RETURN … END FUNCTION.
  • INPUT and OUTPUT for data that comes in or goes out.
  • ← for assignment: max ← value means "max becomes value". The arrow avoids confusion with =, which many people read as "is equal to".
  • Indentation to show which steps belong inside a decision or a loop, and an explicit END line to close it.

Flowcharts

A flowchart draws an algorithm as a diagram: each step is a shape, and arrows show the order in which the steps run. Its strength is that branches and loops are visible at a glance. The shapes are standardized (ISO 5807):

Shape Name Meaning
Oval (rounded ends) Terminator Start or end of the algorithm
Rectangle Process A calculation or assignment
Diamond Decision A yes/no question; two arrows leave it
Parallelogram Input/output Data that comes in or goes out
Arrow Flow line The order of the steps

This is the temperature algorithm as a flowchart:

Flowchart that finds the highest temperature: start, input the temperatures, set max to the first one, then loop over the other values, replacing max when a value is higher, and output max when no values are left. Start Input temperatures max ← first temperature Another value? Yes value > max? Yes max ← value No No Output max End
The loop is visible as the line that runs back up to the decision "Another value?".

Pseudocode and flowcharts describe the same thing. Flowcharts are easier to follow for small algorithms and for people who don't program; pseudocode scales better, because a flowchart with fifty shapes becomes a maze, while fifty lines of pseudocode are still readable — and much closer to the code you will write next.

Trace tables

A trace table is a table in which you record the value of every variable, step by step, while you carry out an algorithm by hand. This is called a dry run: you play the computer. It shows whether the algorithm really produces the right answer, and when it does not, exactly where it goes wrong.

Here is a dry run of the temperature algorithm with the input 14, 19, 11, 22, 17. There is one column per variable and one for the condition; each row is one pass through the loop.

Step value value > max? max
Start — — 14
Loop 1 19 yes 19
Loop 2 11 no 19
Loop 3 22 yes 22
Loop 4 17 no 22
Output — — 22

The output is 22, which is correct. Good test inputs include the awkward cases too: the highest value first, the highest value last, all values equal, and a list with only one value.

You can also let the computer print the trace table for you, by adding a print() at each step. This is one of the simplest and most useful debugging techniques there is:

temperatures = [14, 19, 11, 22, 17]

maximum = temperatures[0]
print("value  higher?  max")
print(f"{'-':>5}  {'-':>7}  {maximum:>3}")
for value in temperatures[1:]:
    higher = value > maximum
    if higher:
        maximum = value
    print(f"{value:>5}  {str(higher):>7}  {maximum:>3}")
value  higher?  max
    -        -   14
   19     True   19
   11    False   19
   22     True   22
   17    False   22

From pseudocode to Python

Good pseudocode translates almost line by line into a real language. Here is the temperature algorithm as a Python function:

def largest(values):
    maximum = values[0]              # max ← first item
    for value in values[1:]:         # FOR EACH value IN the remaining items
        if value > maximum:          #     IF value > max THEN
            maximum = value          #         max ← value
    return maximum                   # OUTPUT max

temperatures = [14, 19, 11, 22, 17]
print(largest(temperatures))
print(largest([3]))
22
3

The variable is called maximum rather than max, because max is already the name of a Python built-in function — which, in real code, is exactly what you would use: max(temperatures). Writing the algorithm yourself is still worthwhile: it is the pattern behind every "find the best item" task, and built-ins do not exist for every problem. The Python syntax used here is explained in Functions and Loops.

Common pitfalls

Ambiguous steps

A step such as "sort the list" or "remove the duplicates" hides decisions: sort ascending or descending? Keep the first duplicate or the last? A computer needs one answer, so an algorithm must state it. If two people can carry out a step and get different results, the step is not precise enough.

A missing end condition

Every loop needs a condition that eventually becomes false. This algorithm is meant to count down from a number to 1, but it never changes n, so n > 0 stays true forever — an infinite loop:

INPUT n
WHILE n > 0
    OUTPUT n
END WHILE

The fix is a step inside the loop that moves toward the end condition: n ← n - 1. A trace table makes this kind of bug obvious, because the value of n in every row is the same.

Input nobody thought of

The temperature algorithm starts with "take the first item". What if the list is empty? There is no first item, and the Python version crashes:

def largest(values):
    maximum = values[0]
    for value in values[1:]:
        if value > maximum:
            maximum = value
    return maximum

print(largest([]))
IndexError: list index out of range

A complete algorithm says what happens for every possible input: here, for example, "if the list is empty, output a message instead". Edge cases like an empty list, a single item, zero or a negative number are where most algorithms break.