Safekipedia

Algorithm

Adventurer · ages 7–10Adapted from Wikipedia
Flowchart of an algorithm to find the greatest common divisor of two numbers.

In mathematics and computer science, an algorithm is a set of clear, exact instructions. It is usually used to solve a certain kind of problem or to do 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 correct inferences, which is known as automated reasoning, and this helps with automation. Alan Turing described machines in human-like ways, using words such as "memory", "search" and "stimulus".

Flowchart of an algorithm to find the greatest common divisor of two numbers.

A heuristic is different. It is a way of solving problems that may not be fully clear. It may not always give the correct or best result, especially when there is no clearly right answer. Recommender systems on social media use heuristics. People often call these "algorithms" in modern media, but they cannot always give correct results.

As an effective method, an algorithm can be written in a small amount of space and time, using a clearly defined formal language, to calculate a function. It starts from a beginning state and input (which may be empty). When it is executed, it moves through a set number of clear steps, then produces an "output" and stops. 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 was called kitāb al-ḥisāb al-hindī ("Book of Indian computation"). The other was 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 books appeared. They explained the Hindu–Arabic numeral system and arithmetic. Two examples are Liber Alghoarismi de practica arismetrice, linked to John of Seville, and Liber Algoritmi de numero Indorum, linked to Adelard of Bath. In these titles, alghoarismi or algoritmi is a Latin form of Al-Khwarizmi's name. The text begins with the phrase Dixit Algoritmi, meaning "Thus spoke Al-Khwarizmi."

Muhammad ibn Musa al-Khwarizmi: The 9th-century mathematician whose name is the origin of the word 'algorithm'.

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, Geoffrey Chaucer wrote The Canterbury Tales. He used a similar word to describe augrym stones, which were used for place-value calculation. In the 15th century, the Latin word was changed to algorithmus. This change was influenced by 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 more detailed look at the different ideas about what "algorithm" means, see Algorithm characterizations.

One simple idea is that an algorithm is a set of clear rules that tells you, step by step, what to do. This includes all computer programs, and any bureaucratic procedure or cook-book recipe. In general, a program is an algorithm only if it stops at some point.

A more exact way to say it is that an algorithm is a clear list of instructions that produces a result. A computer or a person can follow it to work with symbols.

History

People have written down step-by-step methods for solving math problems for a very long time. Examples come from 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 an early division algorithm. Babylonian clay tablets from the Hammurabi dynasty also described methods for calculating formulas. Algorithms were also used in Babylonian astronomy to predict when important sky events would happen. Egyptian arithmetic methods appear in the Rhind Mathematical Papyrus. 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 an 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-1800s, Charles Babbage designed the difference and analytical engines. Ada Lovelace wrote the first algorithm meant for a computer, the analytical engine. That machine 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 built a digital adding device after seeing how hard it was to use mechanical calculators, which were built with gears. Telephone networks had already used electromechanical relays since 1835.

Ada Lovelace's diagram from "Note G", the first published computer algorithm

In 1928, work began on a partial formal definition of the modern idea of algorithms. It came from 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, have tried to describe what an algorithm is using precise mathematical ideas. His work on abstract state machines is one example. 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. These include natural languages, pseudocode, flowcharts, drakon-charts, programming languages, and control tables. Plain language versions can be long and hard to follow, so they are not often used for complicated algorithms. Pseudocode, flowcharts, drakon-charts, and control tables are more organized ways to show algorithms. They help make things clearer. Programming languages are mainly used to write algorithms that computers can run. They are also used to describe or record them.

Turing machine programs can be shown in several ways. One way is a series of machine tables (see finite-state machine, state-transition table, and control table). Another way is with flowcharts and drakon-charts (see state diagram). A third way is with a basic form of machine code or assembly code called "sets of quadruples." A flowchart is a picture that shows and records an algorithm. It uses four main symbols: arrows show the flow of the program, rectangles show steps, diamonds show decisions, and dots join paths together.

Algorithmic analysis

Main article: Analysis of algorithms

Knowing how much time, storage, or other resources an algorithm needs is often important. People have found ways 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)) is faster than a step-by-step 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. Instead, pseudocode is used 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 very important for fast, interactive, business, or long-lasting science uses. Larger inputs often show that slow algorithms are a problem, even when they seem fine at first. Testing can uncover surprising effects, and benchmarks can compare improvements. However, testing cannot fully replace careful analysis. A recent improvement to 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

There are many ways to design an algorithm, such as divide-and-conquer or dynamic programming. These ways of designing and building algorithms are called algorithm design patterns. A key goal is to use resources, such as memory and time, wisely. The big O notation helps show how the amount of resources needed changes as the size of the inputs grows.

Any algorithm can be carried out by a 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.

See also: Software patent

By themselves, algorithms usually cannot be patented. In the United States, a set of simple steps that only uses abstract ideas, numbers, or signals is not counted as a "process". Because of this, algorithms cannot be patented (as in Gottschalk v. Benson). However, practical uses of algorithms can be patented. For example, in Diamond v. Diehr, a simple feedback algorithm that helped treat synthetic rubber was judged patentable.

The patenting of software is a debated topic. Some patents on algorithms have been criticized, especially data compression algorithms, such as Unisys's LZW patent. Also, some cryptographic algorithms have limits on where they can be sent to other countries (see export of cryptography).

Classification

Algorithms can be sorted in several ways. A recursive algorithm calls itself over and over until it reaches a stopping point. It is a common functional programming technique. Iterative algorithms repeat steps using loops or stacks. Some problems work better with one approach than the other. The Tower of Hanoi puzzle is often solved with a recursive method. Algorithms are usually described as running one step at a time on regular computers. Parallel algorithms let several processors work at once. Distributed algorithms use many machines connected by a network. Deterministic algorithms make exact choices at every step. Non-deterministic algorithms solve problems by guessing. Heuristics can make those guesses better. Many algorithms find an exact answer. Approximation algorithms find an answer that is close to the true one. This is helpful 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 how they are designed. This design method is called their paradigm. One method is Brute-force search. It tries every option until it finds the best one, but 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. Prune and search solves one smaller version and uses the binary search algorithm. Problems such as playing chess can be shown on graphs. A graph exploration algorithm, search algorithms, branch and bound, and backtracking are useful for these. Randomized algorithms make some choices by chance. This can help find good answers when exact ones are too hard to get. Some of the fastest approximations need randomness. Whether randomized algorithms with polynomial time complexity can be the fastest is an open question. It is known as the P versus NP problem. Randomized algorithms include Monte Carlo algorithms, which give a correct answer with high probability. RP is a group of these that runs in polynomial time. Las Vegas algorithms always give the correct answer, but their running time is only probably limited. ZPP is one example. Reduction of complexity changes hard problems into better-known ones. It aims for asymptotically optimal methods. One example is 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. Any partial solution that cannot lead to a valid full solution is dropped. For optimization problems, methods include Linear programming. It uses the simplex algorithm and can solve the maximum flow problem. Another method is integer programming, where the unknowns must be integers. Dynamic programming avoids repeating work on overlapping subproblems, as in the Floyd–Warshall algorithm. It uses memoization. Greedy algorithms improve a solution through small changes. They may get stuck at local optima. Examples include Huffman Tree, Kruskal, Prim, and Sollin. Heuristic algorithms get close to the best answer when finding it is too hard. They use 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 any order. To find the answer, you must look at every number in the list. The algorithm starts by guessing that the first number is the largest. Then it checks each of the other numbers. If a number is bigger than the current largest, it becomes the new largest. If the list is empty, there is no largest number.

A more formal version is written in a way 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 found better sorting and hashing algorithms. A paper in Nature reported that AlphaDev found small sorting algorithms that worked 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. It is a coding agent powered by large language models. It can find and improve algorithms for many different purposes. AlphaEvolve uses language models to suggest changes to code. It uses automatic checkers to test possible solutions. It also uses an evolutionary process, which improves promising algorithms over many rounds.

Quiz time!

4 questions about Algorithm

Question 1 of 4

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.