In mathematics and computer science, an algorithm () is any well-defined set of instructions that when followed terminates after a finite number of steps that comprise a solution to a given computational problem. Advanced algorithms may utilize loops and involve many conditionals that decide the next step based on the inputs provided, resulting in long sequences of steps before halting, but all algorithms terminate by definition."A procedure which has all the characteristics of an algorithm except that it possibly lacks finiteness may be called a 'computational method (Knuth 1971:5).
In contrast to algorithms, heuristics might be applied to problems for which it is not possible for an algorithmic solution to exist, e.g., because the problem is undecidable or there is otherwise no way to define the correct or optimal result.David A. Grossman, Ophir Frieder, Information Retrieval: Algorithms and Heuristics, 2nd edition, 2004, For example, although social media recommender systems are erroneously called "algorithms" in popular media, they are accurately classified as heuristics, because there is no objective criteria by which a recommendation produced by such a system can be judged as "correct".
As an effective method, an algorithm can be expressed within both a finite amount of space and time"Any classical mathematical algorithm, for example, can be described in a finite number of English words" (Rogers 1987:2). and in a well-defined formal languageWell defined concerning the agent that executes the algorithm: "There is a computing agent, usually human, which can react to the instructions and carry out the computations" (Rogers 1987:2). for calculating a function."an algorithm is a procedure for computing a function (concerning some chosen notation for integers) ... this limitation (to numerical functions) results in no loss of generality", (Rogers 1977:1). Starting from an initial state and input, a computation occurs at each step, eventually producing output"An algorithm has one or more outputs, i.e., quantities which have a specified relation to the inputs" (Knuth 1973:5). and terminating. Randomized algorithms incorporate random inputs which can be used to simulate non-deterministic output even though the transitions between states are deterministic.Whether or not a process with random interior processes (not including the input) is an algorithm is debatable. Rogers opines that: "a computation is carried out in a discrete stepwise fashion, without the use of continuous methods or analog devices ... carried forward deterministically, without resort to random methods or devices, e.g., dice" (Rogers 1987:2).
Etymology Around 825 AD, Persian scientist and polymath al-Khwarizmi wrote ("Book of Indian computation") and ("Addition and subtraction in Indian arithmetic"). In the early 12th century, Latin translations of these texts involving the Hindu–Arabic numeral system and arithmetic appeared, for example , attributed to John of Seville, and , attributed to Adelard of Bath.Blair, Ann, Duguid, Paul, Goeing, Anja-Silvia and Grafton, Anthony. Information: A Historical Companion, Princeton: Princeton University Press, 2021. p. 247 Here, or is the Latinization of Al-Khwarizmi's name; the text starts with the phrase , or "Thus spoke Al-Khwarizmi".
The word algorism in English came to mean the use of place-value notation in calculations; it occurs in the Ancrene Wisse from circa 1225. By the time Geoffrey Chaucer wrote The Canterbury Tales in the late 14th century, he used a variant of the same word in describing augrym stones, stones used for place-value calculation. In the 15th century, under the influence of the Greek word (, ; cf. "arithmetic"), the Latin word was altered to . By 1596, this form of the word was used in English, as algorithm, by Thomas Hood.
Definition
One informal definition is "a set of rules that precisely defines a sequence of operations", which would include all computer programs, and any bureaucratic procedure or cook-book recipe. In general, a program is an algorithm only if it stops eventually.Stone requires that "it must terminate in a finite number of steps" (Stone 1973:7–8). Formally, algorithm is an explicit set of instructions to produce an output, that can be followed by a computer or a human performing specific operations on symbols.Boolos and Jeffrey 1974, 1999:19
History
Ancient algorithms Step-by-step procedures for solving mathematical problems have been recorded since antiquity. This includes in Babylonian mathematics (around 2500 BC), Egyptian mathematics (around 1550 BC), Indian mathematics (around 800 BC and later),Hayashi, T. (2023, January 1). Brahmagupta. Encyclopedia Britannica. 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 of algorithms is found in ancient Mesopotamian mathematics. A Sumerian clay tablet found in Shuruppak near Baghdad and dated to describes the earliest division algorithm. During the Hammurabi dynasty , Babylonian clay tablets described algorithms for computing formulas. Algorithms were also used in Babylonian astronomy. Babylonian clay tablets describe and employ algorithmic procedures to compute the time and place of significant astronomical events.
Algorithms for arithmetic are also found in ancient Egyptian mathematics, dating back to the Rhind Mathematical Papyrus . Algorithms were later used in ancient Hellenistic mathematics. Two examples are the Sieve of Eratosthenes, which was described in the Introduction to Arithmetic by Nicomachus, and the Euclidean algorithm, which was first described in Euclid's Elements ().Examples of ancient Indian mathematics included the Shulba Sutras, the Kerala School, and the Brāhmasphuṭasiddhānta.
In the 9th century, Muḥammad ibn Mūsā al-Khwārizmī revolutionized the field by establishing the algorithm as a systematic, finite sequence of logical steps to solve mathematical problems. In his influential work, The Compendious Book on Calculation by Completion and Balancing, he moved beyond specific numerical solutions to introduce general procedures for algebraic reduction and balancing. This transformed mathematics into a 'mechanical' process of well-defined rules—a fundamental shift that laid the groundwork for modern algorithmic theory. The Latin translation of his arithmetic treatise, titled Algoritmi de numero Indorum, led to the term algorithm being derived from the Latinization of his name, Algoritmi, specifically to describe this new rule-based approach to mathematics.
The first cryptographic algorithm for deciphering encrypted code was developed by Al-Kindi, a 9th-century Arab mathematician, in A Manuscript On Deciphering Cryptographic Messages. He gave the first description of cryptanalysis by frequency analysis, the earliest codebreaking algorithm.
Computers
Weight-driven clocks Weight-driven clocks were a key European invention in Middle Ages, specifically the verge escapement mechanismBolter 1984:24 producing the tick of mechanical clocks. Accurate automatic machinesBolter 1984:26 led to mechanical automata in the 13th century and computational machines—the difference and analytical engines of Charles Babbage and Ada Lovelace in the mid-19th century.Bolter 1984:33–34, 204–206. Lovelace designed the first algorithm intended for a computer, Babbage's analytical engine, the first real Turing-complete computer, more than the mechanical calculators of the time. Although the full implementation of Babbage's second device was only built decades after her lifetime, Lovelace has been called "history's first programmer".
Electromechanical relay The Jacquard loom, a precursor to punch cards, and telephone switching machines led to the development of the first computers.Bell and Newell diagram 1971:39, cf. Davis 2000 By the mid-19th century, the telegraph, was in use throughout the world. By the late 19th century, ticker tape () and punch cards (c. 1890) were developed. Then came the teleprinter () with its punched-paper use of Baudot code on tape.
Telephone-switching networks of electromechanical relays were invented in 1835. These led to the invention of the digital adding device by George Stibitz in 1937. While working in Bell Laboratories, he observed the "burdensome" use of mechanical calculators with gears, prompting him to create an experimental digital adder at home.Melina Hill, Valley News Correspondent, A Tinkerer Gets a Place in History, Valley News West Lebanon NH, Thursday, March 31, 1983, p. 13.Davis 2000:14
Formalization
In 1928, a partial formalization of the modern concept of algorithms began with attempts to solve David Hilbert's Entscheidungsproblem (decision problem). Later formalizations were framed as attempts to define "effective calculability"Kleene 1943 in Davis 1965:274 or "effective method".Rosser 1939 in Davis 1965:225 Those formalizations included the Gödel–Herbrand–Kleene recursive functions of 1930, 1934 and 1935, Alonzo Church's lambda calculus of 1936, Emil Post's Formulation 1 of 1936, and Alan Turing's Turing machines of 1936–37 and 1939.
Formal theories of algorithms
The mathematical formalization of the notion of algorithm is distinct from the formalization of computable functions. In particular, the equivalence of classical models of computability does not by itself provide a formal definition of an algorithm, since different algorithms can compute the same function. Several approaches have therefore sought to characterize algorithms as mathematical objects.
Yuri Gurevich developed the theory of abstract state machines (ASMs) as a formal characterization of sequential algorithms. His sequential ASM thesis states that every sequential algorithm is behaviorally equivalent to a sequential ASM, with the equivalence established step by step. Gurevich formulated axioms for sequential algorithms and proved the corresponding characterization theorem.
A different approach was developed by Yiannis N. Moschovakis, who proposed a mathematical theory of algorithms based on set-theoretic objects and distinguished algorithms from their implementations. His theory was developed in connection with questions of computational complexity and with the mathematical analysis of particular algorithms.
Modern Algorithms
For decades, it was assumed that algorithm evolution progresses from heuristics to formal algorithms. A Symbolic integration provides a classic illustration. In 1961, James Slagle's program SAINT used heuristics to solve 52 of 54 freshman calculus exercises from an MIT textbook (≈96%). In 1967, Larry Moses's SIN refined the heuristics and achieved 100% success, though it remained heuristic. Finally, in 1969, Robert Risch introduced the Risch Algorithm with formal guarantees. This trajectory defined the traditional path: heuristics evolving until a definitive, guaranteed algorithm emerged.
However, the rise of transformer-based AI has inverted this sequence — classical algorithms are now being displaced by heuristics once again.
Algorithms have evolved and improved in many ways as time goes on. Common uses of algorithms today include social media apps like Instagram and YouTube. Algorithms are used as a way to analyze what people like and push more of those things to the people who interact with them. Quantum computing uses quantum algorithm procedures to solve problems faster. More recently, in 2024, NIST updated their post-quantum encryption standards, which includes new encryption algorithms to enhance defenses against attacks using quantum computing.
Representations