What Is an Algorithm

An algorithm is a precise, step-by-step procedure for solving a problem or completing a task, written clearly enough that it can be followed without any guessing — and it is the idea at the heart of every program, because a program is simply an algorithm written in a language a computer can run. You already use algorithms outside of computers: a recipe is one, directions to a friend's house are one, and the method you learned for long division is one. What makes a procedure a real algorithm is a short list of requirements: it takes some input (the data it starts with), it produces an output (the answer or result), every step is definite (clear enough that no judgment call is needed), and it always finishes after a limited number of steps instead of running forever. The computational-thinking topic described how to break a problem into parts and write the steps as pseudocode; an algorithm is the finished result of that work, the exact sequence of steps you settled on. It is worth separating the algorithm from the program that carries it out: the algorithm is the idea, and the program is one particular way of writing that idea down in Python, JavaScript, or C, so the very same algorithm can be written in many languages and still do the same job. Consider finding the largest number in a list. Start by remembering the first number as the biggest so far, then look at each remaining number in turn, and whenever one is larger than the remembered number, replace the remembered number with it; when the list runs out, the remembered number is the answer. That procedure has an input (the list), an output (the largest number), steps that need no guesswork, and a guaranteed end, because the list is finite. Notice that it uses the building blocks from the programming-basics topic: a variable to remember the biggest value, a loop to visit each item, and a condition to compare. A second famous example is binary search, a way to find a name in a sorted list by checking the middle item and then throwing away the half that cannot contain the target, repeating until it is found, which is how you naturally look up a word in a printed dictionary rather than reading every page from the front. The same problem can usually be solved by more than one algorithm, and the choice matters a great deal: scanning a list from the start works for any list, while binary search needs the list to be sorted first but is dramatically faster, because each check discards half of what remains, so a million items need only about twenty checks instead of up to a million. Computer scientists compare algorithms mainly by how much time and memory they need as the input grows, a measurement the big-O topic covers in more depth, and also by whether the algorithm gives a correct answer in every case, since a fast algorithm that is sometimes wrong is not useful. Correctness is checked by testing it on ordinary inputs and also on awkward edge cases such as an empty list or a list with repeated values, which is exactly the habit the debugging-and-testing topic describes. Algorithms are not always exact recipes with a single right answer either: a search engine uses ranking algorithms to decide which pages to show first, a map app uses a shortest-path algorithm to choose a route, and a machine-learning system uses a learning algorithm that adjusts itself from examples, so the word covers everything from a three-line loop to the large systems that shape what people see online. Because an algorithm decides things, its design can carry real consequences, which is why the computing-ethics topic asks whether the rules and data behind an algorithm treat people fairly. Tying it together, an algorithm is not a mysterious or purely technical idea: it is a clear, finite, step-by-step method that turns an input into an output, written first as an idea or pseudocode and then as a program, and judged by whether it is correct and how efficiently it uses time and memory.

A problem to solve
for example: find the largest number in a list
Input
the data the algorithm starts with
Definite steps, in order
clear enough that no guessing is needed
Finishes after a limited number of steps
it must stop, not run forever
Output: the answer
written as pseudocode, then as a program in any language
Finding the Largest Number, Step by Step remember the biggest so far; replace it whenever a larger one appears input list 4 9 2 12 7 Look at 4 first number, so biggest so far = 4 Look at 9 9 is larger than 4, so biggest so far = 9 Look at 2 2 is not larger than 9, so keep 9 Look at 12 12 is larger than 9, so biggest so far = 12 Look at 7 7 is not larger than 12, so keep 12 List finished. Output: the largest number is 12
Two Algorithms, Same Problem finding one name in a sorted list of 1,000,000 names Scan from the start check name 1, then name 2, then name 3, and so on works on any list, sorted or not up to 1,000,000 checks the work grows in step with the list Binary search check the middle name, then discard the half that cannot match needs the list to be sorted first about 20 checks each check halves what remains both are correct, but they differ enormously in how much work they do choosing between algorithms is a central part of computer science
What Makes a Procedure an Algorithm four requirements, plus how to judge the result Input the data it starts with Output the answer or result produced Definite every step is clear, no guessing needed Finite it stops after a limited number of steps Is it correct? right answer for ordinary inputs and awkward edge cases like an empty list Is it efficient? how much time and memory it needs as the input grows larger

Take what is an algorithm further

Ask the copilot about this term, or have our engineers assess it against your process.