What Is an Algorithm? Definition, Examples, and Key Properties
The Formal Definition
In computer science, an algorithm is defined by five properties first articulated by Donald Knuth in his landmark work, The Art of Computer Programming. These properties distinguish a true algorithm from a vague set of instructions or an informal procedure.
Finiteness requires that an algorithm must always terminate after a finite number of steps. A procedure that could run forever is not an algorithm. This seems obvious, but proving termination for complex algorithms can be surprisingly difficult. The halting problem, identified by Alan Turing in 1936, showed that there is no general method to determine whether an arbitrary program will eventually stop.
Definiteness means each step must be precisely defined with no ambiguity. The instruction "add some salt" is not definite because "some" is vague. The instruction "add 5 grams of NaCl" is definite. In programming terms, every operation must be clear enough that a machine can execute it without interpretation or guesswork.
Input specifies that an algorithm takes zero or more values from a defined set. Some algorithms require no input at all, such as an algorithm that generates the first 100 prime numbers. Others take complex, structured inputs like graphs, matrices, or streams of data.
Output requires that an algorithm produces one or more results. An algorithm that does nothing useful, that produces no output, serves no purpose. The output must have a defined relationship to the input.
Effectiveness demands that every operation be basic enough to be performed exactly and in a finite amount of time. You cannot instruct an algorithm to "find the meaning of life" because that operation is not effective. You can instruct it to "compare two integers and return the larger one" because that operation is basic and completable.
Algorithms in Everyday Language
Before computers existed, people used algorithms constantly. A recipe for cooking a meal is an algorithm: it takes ingredients (input), applies a series of steps (processing), and produces a dish (output). Driving directions from point A to point B form an algorithm. The long division procedure taught in elementary school is an algorithm. What computers added was the ability to execute algorithms at extraordinary speed, processing billions of steps per second.
Consider the simple task of looking up a word in a physical dictionary. You do not start at page one and read every entry. Instead, you estimate where the word falls alphabetically, open the dictionary near that point, check whether you need to go forward or backward, and repeat until you find the word. This is essentially a binary search algorithm, and you perform it intuitively without thinking about computer science.
When you organize a messy desk by grouping papers into categories, you are performing a classification algorithm. When you decide which checkout line to join at a grocery store based on the number of customers and items in each cart, you are running an optimization algorithm in your head. Algorithms are not inventions of the computer age. They are formalized versions of the logical thinking humans have always used to solve problems.
How Algorithms Differ from Programs
An algorithm and a computer program are related but distinct concepts. An algorithm is an abstract description of a procedure, independent of any programming language or hardware platform. A program is a concrete implementation of one or more algorithms in a specific language like Python, Java, or C++.
The same algorithm can be implemented in many different languages and still produce identical results. Merge sort is merge sort whether written in Python or Rust. The algorithm is the logical idea; the program is the physical realization. This distinction matters because it allows computer scientists to analyze and compare algorithms without worrying about implementation details. Two different programs might implement the same algorithm with different coding styles, variable names, and syntactic structures, but they share the same fundamental logic and performance characteristics.
Programs can also contain elements that are not algorithmic, such as user interface code, error handling for hardware failures, or timing delays. An algorithm focuses purely on the computational procedure itself.
Representing Algorithms
Algorithms can be expressed in several ways, depending on the audience and purpose. Natural language descriptions use everyday words, making them accessible but sometimes ambiguous. Pseudocode uses a structured, language-like format that is precise enough to follow but not tied to any specific programming language. Flowcharts use visual diagrams with shapes representing different operations, connected by arrows showing the flow of execution. Mathematical notation uses formal logic and set theory to express algorithms with complete rigor.
Each representation has strengths. Natural language works well for high-level overviews. Pseudocode is ideal for communicating algorithms between programmers who may use different languages. Flowcharts help visualize branching logic and loops. Mathematical notation is necessary for formal proofs of correctness and complexity analysis.
In practice, algorithms are most commonly communicated using pseudocode or actual source code, with natural language explanations providing context. Research papers typically use a combination of mathematical notation, pseudocode, and prose to present algorithms clearly and rigorously.
Categories of Algorithms
Algorithms are classified by the type of problem they solve. Sorting algorithms arrange data in order. Search algorithms find specific items in collections. Graph algorithms solve problems involving networks and relationships. Optimization algorithms find the best solution from a set of possibilities. Cryptographic algorithms secure data through mathematical transformations. Machine learning algorithms identify patterns in data and make predictions.
Algorithms are also classified by their design strategy. Divide and conquer algorithms split problems into smaller pieces, solve each piece, and combine results. Dynamic programming algorithms solve complex problems by breaking them into overlapping subproblems and storing solutions to avoid redundant computation. Greedy algorithms make locally optimal choices at each step, hoping to find a global optimum. Backtracking algorithms systematically explore possibilities and abandon paths that cannot lead to valid solutions.
Understanding these categories helps you recognize which approach is most appropriate for a given problem. A sorting problem calls for a sorting algorithm. A shortest-path problem calls for a graph algorithm. Choosing the right category is the first step in solving any computational challenge.
Why Algorithm Efficiency Matters
Two algorithms that solve the same problem can differ enormously in how much time and memory they require. Linear search checks every element in a list one by one, requiring up to n comparisons for a list of n elements. Binary search on a sorted list requires at most log2(n) comparisons. For a list of one million elements, linear search might need one million comparisons, while binary search needs at most 20. That is a factor of 50,000.
This difference grows even more dramatic with larger inputs. An O(n squared) sorting algorithm processing ten million records performs roughly 100 trillion operations, while an O(n log n) algorithm performs roughly 230 million. The first could take hours; the second might finish in seconds. At the scale of modern data, from social media platforms processing billions of posts to scientific simulations with trillions of data points, algorithm efficiency is not a luxury. It is a necessity.
Computer scientists use Big-O notation to express efficiency in a hardware-independent way. Rather than saying "this algorithm takes 3.2 seconds on my laptop," they say "this algorithm runs in O(n log n) time," which describes how performance scales as input grows. This abstraction lets researchers and engineers compare algorithms without being distracted by differences in processor speed, memory architecture, or programming language overhead.
An algorithm is a precise, finite procedure that transforms inputs into outputs. Understanding algorithms means understanding the logical foundations of all computing, from the simplest calculator to the most advanced AI system.