Skip to content
Eduardo Lira

FUNDAMENTOS

What is an algorithm: properties and types, revisiting what I wrote in 2017

The second article of the intro-to-programming series, from 2017, with the properties and types of algorithms. Rereading it, two of the properties I would write differently today.

This is the second article of the intro-to-programming series I started in 2017. In the first one, #1 - What is a program?, the idea was that a program is a sequence of instructions that says how to carry out a computing operation. Here the subject is what comes before the program: the algorithm.

The definition I used back then still works:

An algorithm is a set of steps that defines how a task will be carried out.

For many centuries, algorithms belonged to mathematics: steps on numbers, almost always written as formulas. Yet we follow algorithms all the time without calling them that. Baking a cake, getting to work, logging into a social network: each is a sequence of steps with a goal. What changes on a computer is that whoever executes the steps interprets nothing. That’s why the instruction has to be understandable and exact for whoever will carry it out.

Properties

In 2017 I listed four properties. I keep the list, with what I would correct in each one.

  1. Finiteness. I called it “effectiveness”, but what I described was something else: the execution of the algorithm has to end. In the original text I already mentioned the exception, data collection systems that repeat the same sequence without stopping. Today I would say those are programs that run an algorithm inside an infinite loop on purpose; each turn of the loop ends.

  2. Precision. At every step, whoever executes it must have enough information and know exactly what to do. The steps need to be simple enough to leave no doubt. The example I gave: to calculate the area of a rectangle, anyone has to know how to multiply and understand that the “x” means multiplication. That’s why the way the algorithm is written matters so much. Deep down, it’s the choice of language.

  3. Generality. I called it “massiveness” and said it applied “after compiling”, which has nothing to do with it: compiling is about programs, not algorithms. The right idea is that the algorithm solves a class of problems, not a single case. The rectangle area algorithm works for any width and any height.

  4. Determinism. Given the same input, the algorithm always produces the same result. I wrote that a process depending on a coin toss could not be called an algorithm. For someone just starting out, the rule helps. But there are algorithms that use randomness on purpose, called probabilistic or randomized, and they are used for real, for example to pick the pivot in a sort. I wouldn’t leave the sentence so absolute.

Types

Here the 2017 text still stands. There are three ways to organize the steps, and almost every program mixes all three.

Linear

The steps run in the order they were written, one after the other, without repeating or skipping anything.

read the width
read the height
compute area = width x height
show the area

With repetition

Sometimes a group of steps has to be repeated while a condition is true. This way of organizing is called a cycle, or loop, and an algorithm that has loops is called cyclic or repetitive.

while there are numbers in the list
    add the number to the total
show the total

When the condition never stops being true, the loop never ends. In 2017 I called this “looping”; the more common name is infinite loop. Apart from the deliberate cases in item 1, it’s a bug, and the algorithm has to be written so it doesn’t fall into one.

With branching

You rarely know the whole sequence of steps in advance. In life the decision depends on the situation: if it rains, an umbrella; if it’s hot, light clothes. In an algorithm, whether a step runs depends on a condition:

IF <condition> DO <action 1> ELSE <action 2>

The second action may not exist:

IF <condition> DO <action 1>

This way of organizing, in which one sequence of steps or another runs depending on the condition, is called branching.

What remains

The 2017 text ended by saying the subject was much broader and that the series would continue. It stopped at this second article; the natural next step, variables, only got a post in 2024. Of the corrections above, the one most worth keeping is the one about determinism: the rule is good for starting out, but it isn’t a law of computing.

Who writes this

Eduardo Lira

Software developer in Rio de Janeiro. I started this blog in 2012, in technical support at a hosting company, writing down the fixes for the problems that came up every day. Today I work at Sofis Tecnologia, on critical healthcare systems and SQL Server database migrations.

Found a mistake in this post?

A command that no longer works, a broken link, wrong information: tell me what you saw. The message goes only to the author and is not shown on the site.