coldwa.st
All guidesProgrammingWebDataToolsDatabasesHaskellConceptsCabal & buildsToolchainCompilerPerformanceEditor & HLS

Programming · concepts · fundamentals

What is an algorithm?

By ColdwastUpdated Jun 14, 20267 min read#algorithm#concepts#cs
Streams of green code on a dark screen
Streams of code on a screen — an algorithm is the step-by-step logic that code like this carries out.

It is one of the most-used words in tech — "the algorithm decides", "a faster algorithm", "algorithmic feed" — and one of the least precisely explained. An algorithm is a simple, fundamental idea that sits underneath all of computing. This guide explains what an algorithm is, its key properties, examples from everyday life and code, why efficiency matters, and how it differs from a program.

The short definition

An algorithm is a finite, step-by-step procedure for solving a problem or producing an output from an input. Give it the same input and follow the steps exactly, and it produces the same result every time. It is a precise recipe — independent of any particular programming language.

An everyday analogy

A cooking recipe is an algorithm: a defined list of steps ("chop the onions, heat the oil, add and stir for 5 minutes") that turns inputs (ingredients) into an output (a dish). So are the instructions for long division you learned at school, or the steps to assemble flat-pack furniture. The idea long predates computers — the word comes from the 9th-century mathematician al-Khwarizmi.

The key properties

  • Well-defined steps — each instruction is clear and unambiguous.
  • Finite — it must end after a limited number of steps, not run forever.
  • Defined input and output — it takes zero or more inputs and produces a result.
  • Effective — each step is basic enough to actually be carried out.

An algorithm is also language-independent: the same sorting algorithm can be written in Python, Haskell, Rust or by hand on paper. The algorithm is the idea; the code is one expression of it.

Colorful source code on a monitor
Colorful source code on a monitor — code is one concrete expression of an algorithm, the underlying step-by-step idea.

A concrete example

To find the largest number in a list, the algorithm is:

1. Assume the first number is the largest so far.
2. Look at each remaining number in turn.
3. If it is bigger than the largest so far, make it the new largest.
4. After the last number, the largest so far is the answer.

That is a complete algorithm — written in plain English, no code required. Translating it into a language is a separate, mechanical step.

Why efficiency matters: Big-O

Different algorithms can solve the same problem with very different amounts of work. Big-O notation describes how an algorithm's cost grows as the input gets larger. Scanning a list once to find the largest value is O(n) — the work grows in proportion to the number of items. A naive approach that compares every pair would be O(n²) — far slower for large inputs. Choosing a better algorithm often matters more than a faster computer: an O(n log n) sort will beat an O(n²) one on a big list every time.

Algorithm vs program

They are related but not the same. An algorithm is the abstract method — the steps. A program is a concrete implementation of one or more algorithms in a specific language, with all the real-world details (input handling, errors, memory) filled in. You design the algorithm first, then implement it. The same algorithm can power many programs.

FAQ

Is an algorithm the same as code? No. The algorithm is the language-independent set of steps; code is one implementation of it. The same algorithm can be written in many languages.

What makes an algorithm "good"? Correctness first (it produces the right output for every valid input), then efficiency (it uses an acceptable amount of time and memory, described by Big-O), then clarity.

What is Big-O notation? A way to describe how an algorithm's running time or memory grows as the input size grows — e.g. O(n) linear, O(n log n), O(n²) quadratic. It compares algorithms independently of the exact machine.

Do I need maths to understand algorithms? Basic logic is enough to start. The everyday ones — search, sort, counting — are intuitive; the maths becomes useful when you analyse and compare their efficiency.

Algorithms are the "what to do"; a language like Haskell or the data carried by an API is the "how" and the "with what". Browse more clear explainers in our guides index.

Independent, community-maintained guide. coldwa.st is a programming-resources site; this article is new, original explanatory writing about algorithms. Examples are illustrative; verify complexity claims against a reference text when it matters.