Algorithm
In mathematics and computer science, an algorithm is a finite sequence of mathematically rigorous instructions. It is usually used to solve a certain kind of problem or to perform a computation. Algorithms are used as step-by-step plans for calculations and data processing.
More advanced algorithms can use conditionals to send the code along different paths. This is called automated decision-making. They can also reach valid inferences, which is known as automated reasoning, and this helps reach automation. Alan Turing already described machines in human-like ways, using words such as "memory", "search" and "stimulus".
A heuristic is different. It is a way of solving problems that may not be fully specified or may not guarantee correct or best results, especially when there is no clearly correct answer. Recommender systems on social media rely on heuristics. Although they are often called "algorithms" in 21st-century popular media, they cannot always give correct results because of the nature of the problem.
As an effective method, an algorithm can be written within a limited amount of space and time, using a clearly defined formal language, to calculate a function. It starts from an initial state and input (which may be empty). When it is executed, it moves through a set number of clear steps, eventually producing an "output" and then stopping. The move from one step to the next is not always deterministic. Some algorithms, called randomized algorithms, use random input.
For the study of algorithms as a subfield of computer science, see Analysis of algorithms. For other uses, see Algorithm (disambiguation).
Etymology
Around 825 AD, Persian scientist and polymath al-Khwarizmi wrote two books, one called kitāb al-ḥisāb al-hindī ("Book of Indian computation") and the other called kitab al-jam' wa'l-tafriq al-ḥisāb al-hindī ("Addition and subtraction in Indian arithmetic"). In the early 12th century, Latin translations of these texts appeared. They dealt with the Hindu–Arabic numeral system and arithmetic. Two examples are Liber Alghoarismi de practica arismetrice, attributed to John of Seville, and Liber Algoritmi de numero Indorum, attributed to Adelard of Bath. In these titles, alghoarismi or algoritmi is the Latinization of Al-Khwarizmi's name. The text begins with the phrase Dixit Algoritmi, meaning "Thus spoke Al-Khwarizmi."
The English word algorism came to mean the use of place-value notation in calculations, where a digit's position gives its value. It appears in the Ancrene Wisse from around 1225. In the late 14th century, when Geoffrey Chaucer wrote The Canterbury Tales, he used a variant of the same word to describe augrym stones, which were used for place-value calculation. In the 15th century, the Latin word was changed to algorithmus under the influence of the Greek word ἀριθμός (arithmos, meaning "number"), which is also the source of the word "arithmetic." By 1596, Thomas Hood was using this form of the word in English as algorithm.
Definition
For a detailed presentation of the various points of view on the definition of "algorithm", see Algorithm characterizations.
One simple idea is that an algorithm is "a set of rules that precisely defines a sequence of operations." This includes all computer programs, and any bureaucratic procedure or cook-book recipe. In general, a program is an algorithm only if it stops eventually.
More formally, an algorithm is an explicit set of instructions that produces an output. A computer or a human can follow it to perform specific operations on symbols.
History
People have written down step-by-step methods for solving math problems since ancient times, including Babylonian mathematics (around 2500 BC), Egyptian mathematics (around 1550 BC), Indian mathematics (around 800 BC and later), the Ifa Oracle (around 500 BC), Greek mathematics (around 240 BC), Chinese mathematics (around 200 BC and later), and Arabic mathematics (around 800 AD). The earliest evidence comes from ancient Mesopotamian mathematics. A Sumerian clay tablet found in Shuruppak near Baghdad, dated to around 2500 BC, describes the earliest division algorithm. Babylonian clay tablets from the Hammurabi dynasty also described methods for computing formulas, and algorithms were used in Babylonian astronomy to work out when important sky events would happen. Egyptian arithmetic methods appear in the Rhind Mathematical Papyrus, and later Hellenistic mathematics used algorithms such as the Sieve of Eratosthenes, described in the Introduction to Arithmetic by Nicomachus, and the Euclidean algorithm, first described in Euclid's Elements. Ancient Indian examples include the Shulba Sutras, the Kerala School, and the Brāhmasphuṭasiddhānta. In the 9th century, Muḥammad ibn Mūsā al-Khwārizmī described the algorithm as a clear, step-by-step set of rules for solving math problems in his book The Compendious Book on Calculation by Completion and Balancing. The word "algorithm" comes from the Latin form of his name. Also in the 9th century, Al-Kindi wrote the first known description of cryptanalysis by frequency analysis, an early code-breaking method. Later, weight-driven clocks in the Middle Ages used the verge escapement, and mechanical automata appeared in the 13th century. In the mid-19th century, Charles Babbage designed the difference and analytical engines, and Ada Lovelace designed the first algorithm meant for a computer, the analytical engine, which was the first real Turing-complete computer and was more advanced than the mechanical calculators of the time. Lovelace has been called "history's first programmer." The Jacquard loom, an early form of punch cards, and telephone switching machines helped lead to the first computers. The telegraph, ticker tape, and the teleprinter, which used Baudot code, were also developed. In 1937, George Stibitz created a digital adding device after seeing how hard it was to use mechanical calculators, which were built with gears, and telephone networks had already used electromechanical relays since 1835.
In 1928, work began on a partial formalization of the modern idea of algorithms, through attempts to solve David Hilbert's Entscheidungsproblem (decision problem). Later attempts to define "effective calculability" included the Gödel–Herbrand–Kleene recursive functions, Alonzo Church's lambda calculus, Emil Post's Formulation 1, and Alan Turing's Turing machines. Other researchers, such as Yuri Gurevich's work on abstract state machines, have tried to describe what an algorithm is in precise mathematical terms. Algorithms have also changed a great deal over time. Today, social media apps like Instagram and YouTube use algorithms to learn what people like and show them more of it. Quantum computing uses special procedures to solve some problems faster. In 2024, NIST updated its post-quantum encryption standards with new algorithms designed to defend against attacks from quantum computers.
Representations
Algorithms can be written in many ways, including natural languages, pseudocode, flowcharts, drakon-charts, programming languages, or control tables. Natural language versions tend to be long and unclear, so they are rarely used for complex or technical algorithms. Pseudocode, flowcharts, drakon-charts, and control tables are structured ways to show algorithms that avoid this confusion. Programming languages are mainly used to write algorithms that computers can run, but they are also used to define or record them.
Turing machine programs can be shown as a series of machine tables (see finite-state machine, state-transition table, and control table), as flowcharts and drakon-charts (see state diagram), or as a basic form of machine code or assembly code called "sets of quadruples." A flowchart is a picture that describes and records an algorithm. It uses four main symbols: arrows for the flow of the program, rectangles for steps, diamonds for decisions, and dots for joining paths.
Algorithmic analysis
Main article: Analysis of algorithms
Knowing how much time, storage, or other cost an algorithm may need is often important, so methods have been developed to estimate these needs. For example, an algorithm that adds up a list of n numbers needs time O(n), using big O notation. It only needs to remember two values: the sum so far and its current position in the list. If the space used to store the input numbers is not counted, it needs space O(1). Otherwise, it needs O(n). Different algorithms can finish the same task using more or less time, space, or 'effort'. For example, a binary search (cost O(log n)) works faster than a sequential search (cost O(n)) for table lookups on sorted lists.
The study of algorithms is part of computer science. Algorithms are often studied without a specific programming language, using pseudocode as a simple, general way to show them. Most algorithms run on real computers and programs, and their algorithmic efficiency is tested with real code. A slow algorithm may not matter for a one-time problem, but it can be critical for fast, interactive, commercial, or long-lasting scientific uses. Larger inputs often reveal slow algorithms that otherwise seem harmless. Testing can uncover unexpected effects, and benchmarks can compare improvements, but testing cannot fully replace formal analysis. A recent innovation for FFT algorithms used in image processing can cut processing time by up to 1,000 times for medical imaging. The best case of an algorithm is the input where it takes the least time and resources. The worst case is the input where it uses the most.
Main articles: Empirical algorithmics, Profiling (computer programming), and Program optimization
Main article: Best, worst and average case
Design
See also: Algorithm § By design paradigm
Algorithms can be designed in many different ways, such as divide-and-conquer or dynamic programming. The ways of designing and building algorithms are also called algorithm design patterns. An important goal is to use resources, such as memory and time, well. The big O notation is used to describe how the amount of resources used changes as the size of the inputs grows.
Any algorithm can be computed by any Turing complete model, which needs only four kinds of instructions. Tausworthe added two more structures, DO-WHILE and CASE, to the three Böhm-Jacopini canonical structures: SEQUENCE, IF-THEN-ELSE, and WHILE-DO. A structured program is also easier to check using proofs of correctness and mathematical induction.
Legal status
See also: Software patent
By themselves, algorithms usually cannot be patented. In the United States, a claim made only of simple steps with abstract ideas, numbers, or signals does not count as a "process", so algorithms cannot be patented (as in Gottschalk v. Benson). However, practical uses of algorithms can be patentable. For example, in Diamond v. Diehr, using a simple feedback algorithm to help cure synthetic rubber was judged patentable.
The patenting of software is controversial. Some patents on algorithms have been criticized, especially data compression algorithms, such as Unisys's LZW patent. Also, some cryptographic algorithms have export restrictions (see export of cryptography).
Classification
Algorithms can be sorted in several ways. A recursive algorithm calls itself again and again until it meets a stopping point, and it is a common functional programming technique. Iterative algorithms repeat steps using loops or stacks. Some problems suit one approach better than the other. The Tower of Hanoi puzzle is often solved with a recursive method. Algorithms are usually discussed as running one instruction at a time on serial computers, but parallel algorithms let several processors work at once, and distributed algorithms use many machines connected by a network. Deterministic algorithms make exact decisions at every step, while non-deterministic algorithms solve problems by guessing, and heuristics can make those guesses more accurate. Many algorithms find an exact answer, but approximation algorithms find an answer that is close to the true one, which is useful for hard problems like the Knapsack problem. Quantum algorithms run on a model of quantum computation and use features of Quantum computing such as quantum superposition or quantum entanglement.
Algorithms can also be grouped by their design method, called their paradigm. One method is Brute-force search, which tries every option until the best one is found, though this can take a long time. A divide-and-conquer algorithm breaks a problem into smaller versions of itself, often recursively, as in Merge sorting, while prune and search solves one smaller version and includes the binary search algorithm. Problems such as playing chess can be modelled on graphs, which is where a graph exploration algorithm, search algorithms, branch and bound, and backtracking are useful. Randomized algorithms make some choices by chance, which can help find good answers when exact ones are impractical; some of the fastest approximations need randomness, and whether randomized algorithms with polynomial time complexity can be fastest is an open question known as the P versus NP problem. Randomized algorithms include Monte Carlo algorithms, which give a correct answer with high probability (with RP being a subclass that runs in polynomial time), and Las Vegas algorithms, which always give the correct answer but whose running time is only probably bounded (such as ZPP). Reduction of complexity changes hard problems into better-known ones, aiming for asymptotically optimal methods, as in a selection algorithm that sorts a list first and then picks the middle element; this is also called transform and conquer. In Back tracking, solutions are built step by step and dropped when they cannot lead to a valid full solution. For optimization problems, methods include Linear programming, which uses the simplex algorithm and can solve the maximum flow problem, and integer programming, where unknowns must be integers. Dynamic programming avoids repeating work on overlapping subproblems, as in the Floyd–Warshall algorithm, and uses memoization. Greedy algorithms improve a solution through small changes and may stop at local optima, with examples including Huffman Tree, Kruskal, Prim, and Sollin. Heuristic algorithms get close to the best answer when finding it is impractical, using methods such as local search, tabu search, simulated annealing, and genetic algorithms.
Examples
Further information: List of algorithms and List of artificial intelligence algorithms
One of the simplest algorithms finds the largest number in a list of numbers in random order. To find the answer, you must look at every number in the list. The algorithm starts by assuming the first number is the largest. It then checks each remaining number, and if a number is greater than the current largest, that number becomes the new largest. If the list is empty, there is no largest number.
A more formal version is written in prose that is much closer to a computer program, using pseudocode or pidgin code.
AI-assisted algorithm discovery
See also: AlphaTensor
Artificial intelligence systems have been used to find and improve algorithms. In 2023, Google DeepMind introduced AlphaDev, a reinforcement learning system based on AlphaZero. AlphaDev discovered better sorting and hashing algorithms. In a paper published in Nature, it was reported that AlphaDev found small sorting algorithms that did better than the best methods people had found before. These algorithms were added to the LLVM standard C++ sorting library.
In 2025, Google DeepMind introduced AlphaEvolve, a coding agent powered by large language models that can find and improve algorithms for many different purposes. AlphaEvolve uses language models to suggest changes to code, automatic checkers to test possible solutions, and an evolutionary process to improve promising algorithms over many rounds.

Quiz time!
5 questions about Algorithm
What is an algorithm?
Keep exploring
This article is a child-friendly adaptation of the Wikipedia article on Algorithm, available under CC BY-SA 4.0.
Images from Wikimedia Commons. Tap any image to view credits and license.