Programming concepts
What is an Algorithm?
Definition
An algorithm is a finite, unambiguous sequence of steps that takes an input and produces a desired output.
Algorithm in more detail
Three properties are load-bearing. It must be finite, so it terminates rather than running forever. Each step must be unambiguous, so it can be followed mechanically without judgement. And it must be defined in terms of inputs and outputs, so it can be reasoned about independently of the machine that runs it. Long division is an algorithm; so is a recipe, in the loose sense; so is binary search, precisely.
An algorithm is distinct from its implementation. Quicksort is one algorithm with thousands of implementations in different languages, and you can compare it with mergesort or heapsort without writing any of them — which is the point of analysing complexity in terms of input size rather than seconds. It is also distinct from a heuristic, which trades a guarantee of the best answer for speed, and from a model, which is learned from data rather than specified in steps.
In everyday engineering the choice of algorithm is usually settled by choosing a data structure. Looking something up in a list is linear; in a hash map it is constant; keeping items sorted while inserting suggests a tree. The colloquial 'the algorithm', meaning a platform's recommendation system, is a different sense of the word entirely — those are trained models with many components, not a sequence of steps anyone wrote down.
Algorithm example
Binary search — halve the range each step
input: a SORTED array and a target
1. low = 0, high = n - 1
2. while low <= high:
mid = (low + high) / 2, rounded down
if a[mid] == target -> return mid
if a[mid] < target -> low = mid + 1
else -> high = mid - 1
3. return not found
Terminates: the range shrinks every iteration.
Unambiguous: no step requires a decision.
1,000,000 items -> at most 20 comparisons (O(log n))
Linear search on the same data: up to 1,000,000 comparisons.
Same problem, same hardware, same language — different algorithm.Algorithm — common questions
- What is the difference between an algorithm and a program?
- An algorithm is the method, independent of any language; a program is one concrete implementation of it. The same algorithm can be written in any language, and a single program usually implements many algorithms.
- Do I need to know algorithms for everyday development?
- You need the vocabulary and the instincts more than the implementations. Knowing why a hash map lookup beats scanning a list, or why sorting inside a loop is a trap, changes the code you write daily; hand-writing a red-black tree almost never does.
- Is an algorithm the same as the recommendation feed people talk about?
- No. That colloquial use refers to a large machine-learned ranking system with many components. A classical algorithm is a specified sequence of steps whose behaviour can be proved; a learned model's behaviour is derived from data.
Keep reading
Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.
Browse the A-Z glossary