What Is an Algorithm? A Beginner-Friendly Explanation
By the BrainSnail editorial team. How these articles are written and checked, and how to tell us when one is wrong.
What is an algorithm, and why does the word appear so often when people talk about computers? An algorithm is a clear sequence of steps for completing a task or solving a problem. You use algorithm-like thinking in ordinary life too, whenever you follow a recipe, sort a list, or choose what to do based on a condition.
The idea in simple terms
An algorithm takes some starting information, follows defined instructions, and produces a result. A computer program may contain many algorithms working together. One algorithm could sort names into alphabetical order, another could search for a particular item, and another could decide what happens after you press a button.
The steps need to be precise enough to follow. Telling a computer to 'sort these numbers nicely' is not useful because 'nicely' is vague. Telling it to compare values and move smaller ones before larger ones gives a process that can be carried out. This precision is one reason algorithm design teaches careful thinking about what a problem actually requires.
When you ask what an algorithm is, remember that an algorithm is not the same thing as a programming language. The algorithm is the method. Code is one way to express that method so a computer can perform it. The same basic algorithm can often be written in several programming languages.
Algorithms can make choices and repeat steps
Many algorithms are more than a straight list from step one to step ten. They can include conditions. A navigation app, for example, might choose one action if a road is open and another if it is closed. A quiz can check whether an answer is correct and then follow different steps depending on the result.
Algorithms can also use loops, which repeat instructions until a condition changes or a set number of repetitions is reached. A program might inspect every item in a list one by one. Instead of writing the same instruction hundreds of times, the algorithm describes the repeated pattern once.
This is useful for understanding an algorithm because real problems often contain both choices and repetition. The power comes from arranging simple instructions so they handle many possible inputs without needing a completely new plan each time.
How to judge a useful algorithm
When comparing methods, check a few basic questions:
- •Does the algorithm produce the correct result?
- •Are the steps clear and unambiguous?
- •Does it stop when the task is complete?
- •How much time does it need as the input grows?
- •How much memory or other resources does it use?
Algorithms also appear in problems where there is no single perfect answer. A route-finding method may search many possible paths and choose one that is short enough to be useful, while a recommendation system may rank options using several signals. In those cases, designers must decide what counts as a good result. That matters when you study an algorithm because the steps reflect human choices about goals, priorities, and trade-offs. An efficient method can still produce poor outcomes if the problem was defined badly or the input data is incomplete.
Two ways to find a number
A classic example shows how the choice of method matters. Suppose a friend picks a whole number from 1 to 100 and answers only higher or lower. One algorithm guesses 1, then 2, then 3, and so on. This is linear search, and in the worst case it needs a hundred guesses. It is simple and always works, even on a jumbled list, but it gets slower in direct proportion to the size of the problem.
A better algorithm starts at 50. If the answer is higher, every number from 1 to 50 is gone in one step, and the next guess is the middle of what remains. This is binary search, and it never needs more than seven guesses for a hundred numbers. Doubling the range to two hundred adds only one more guess. The catch is that binary search only works when the items are already in order.
A word from Baghdad and a method from Euclid
The word algorithm comes from the name of Muhammad ibn Musa al-Khwarizmi, a ninth century scholar who worked in Baghdad. His book explaining calculation with the Hindu-Arabic numerals, including zero and place value, was translated into Latin, and the Latin versions began with his name rendered as Algoritmi. Over time the name became attached to step-by-step methods of calculation in general. Another of his books gave us the word algebra, from al-jabr, a term in its title describing an operation for rearranging equations.
One of the oldest algorithms still in daily use appears in Euclid's Elements, written around 300 BC. It finds the greatest common divisor of two numbers, the largest number that divides both exactly. Take 48 and 18. Divide 48 by 18 to get 2 with remainder 12. Now divide 18 by 12, giving remainder 6. Then divide 12 by 6, giving remainder 0. The last non-zero remainder, 6, is the answer. Each step makes the numbers smaller, so the method is guaranteed to stop, and a version of it is used in modern encryption.
When the obvious method fails
Some algorithms work by always taking the choice that looks best right now. These are called greedy algorithms, and they are often fast and simple. Giving change is the classic case. To make 68p from British coins, a greedy method takes the largest coin that fits each time: 50p, then 10p, then 5p, 2p and 1p, five coins in all. For British coins and many other real currencies, this always gives the fewest coins possible, which is why a shop assistant never needs to think twice about it.
Now imagine a currency with coins worth 1, 3 and 4. To make 6, the greedy method grabs a 4, then has to add two 1s, using three coins, yet two 3s would do the job with only two. The fix is a different strategy called dynamic programming. Instead of committing early, it works out the best answer for every smaller amount first and builds on those answers: 1 needs one coin, 2 needs two, 3 and 4 need one each, 5 needs two, and so 6 needs two. The lesson is that a method that feels sensible still has to be shown to be correct.
Problems that outgrow any computer, and one with no algorithm
Some problems have obvious algorithms that are useless in practice. A delivery driver wants the shortest round trip through a set of towns, a puzzle known as the travelling salesman problem. One correct method is to list every possible route and pick the shortest. With 5 towns there are only 12 distinct round trips. With 10 towns there are 181,440. With 20 there are about 60 thousand million million, and a computer checking a billion routes every second would need nearly two years. Add five more towns and the wait grows to around ten million years.
Programmers tackle such problems with clever shortcuts that find excellent routes without checking them all, although nobody has proved whether a fundamentally fast exact method exists. Some questions are worse still, because they have no algorithm at all. In 1936 the British mathematician Alan Turing proved that no general method can examine any program, together with its input, and always decide correctly whether that program will eventually stop or run forever. This is known as the halting problem. It means that some tasks are not merely slow for computers but impossible to solve in general, however powerful the machine.
The takeaway
An algorithm is a defined method for turning a problem into a sequence of workable steps. Algorithms can include decisions, loops, comparisons, and calculations, and they can be expressed in code or explained before any code is written. Focus on the method first, then ask whether it is correct, clear, and efficient for the job.