What is an algorithm? An algorithm is a finite, clearly described sequence of steps for solving a problem or completing a task. It takes defined inputs, follows instructions in a particular order and produces an output or result. Algorithms are used in computing, mathematics, science, logistics, navigation, cooking and everyday decision-making. A computer program is written code; an algorithm is the underlying method the code is meant to carry out.
A useful way to understand an algorithm is to think about a recipe. A recipe gives ingredients, actions and an order: prepare, mix, heat and serve. A computer algorithm works in a similar structured way, but its instructions must be precise enough for the machine and the data involved. ‘Sort these names’ is a goal; an algorithm explains exactly how the sorting is performed.
Quick notes: An algorithm has a defined problem, inputs, steps and outputs. Good algorithms are unambiguous, finite and appropriate for the task. Flowcharts and pseudocode help plan them before coding. Different algorithms can solve the same problem with different speed, memory use and reliability. An algorithm is not the same thing as a programming language, app, search engine or artificial intelligence model.
What makes a set of instructions an algorithm?
An algorithm needs to describe a process sufficiently clearly that it can be followed. Its steps should not depend on guesswork about what the instruction means. It should also have a stopping condition for the task at hand. An instruction such as ‘keep improving the answer until it is perfect’ is not a complete algorithm because ‘perfect’ is undefined and the process may never finish.
A simple algorithm can be short. To find the larger of two numbers, compare them and report the one that is greater, or report that they are equal. A more complex algorithm may contain thousands of steps and make decisions based on large amounts of data. The central idea remains the same: a defined method transforms inputs into a result.
The five basic building blocks
1. Sequence
Sequence means that instructions are carried out in an intended order. If a program calculates an average, it must obtain the values, add them, count them and divide the total by the count. Dividing before finding the total would produce a different result.
2. Selection
Selection means choosing between alternative actions according to a condition. An algorithm might ask whether a number is greater than zero, then follow one route if the answer is yes and another if it is no. In programming, this commonly appears as an if/else statement.
3. Iteration
Iteration means repeating a set of instructions. A loop can process every item in a list, count from one to ten or continue while a condition remains true. Every loop needs a sensible way to progress towards stopping; otherwise an unintended infinite loop can occur.
4. Input and output
An algorithm receives information and produces a result. Inputs might be numbers, words, dates, images, sensor readings or user choices. Outputs might be a calculation, a sorted list, a decision or a message. Some algorithms also update stored information as part of their operation.
5. State and variables
A process often needs to remember information as it runs. A variable can hold a current total, the position being examined or the best value found so far. The algorithm changes this state in a controlled way as it processes information.
A worked example: find the largest number
Suppose a teacher has a list of practice scores and wants to identify the largest. A simple algorithm can begin by treating the first score as the largest seen so far. It then checks each remaining score. Whenever it finds a larger value, it updates the stored largest value. At the end, it reports the result.
- Start: take the first score and store it as
largest. - Repeat: examine each remaining score one at a time.
- Compare: if the current score is greater than
largest, replacelargestwith the current score. - Finish: after every score has been checked, output
largest.
scores = [65, 82, 74, 91, 88]
largest = scores[0]
for score in scores[1:]:
if score > largest:
largest = score
print(largest) # 91
The code is one implementation of the algorithm. The algorithm itself can be described in ordinary language, drawn as a flowchart or written in another programming language. Notice the assumption: the list must contain at least one score. A complete solution should define what happens if the list is empty.
What are pseudocode and flowcharts?
Pseudocode describes an algorithm using structured, readable language without requiring the exact syntax of a specific programming language. It allows a learner to concentrate on the logic rather than brackets, punctuation or indentation. Pseudocode has no single universal format, so clarity and consistency are more important than a particular set of keywords.
SET largest TO first score
FOR each remaining score
IF score > largest THEN
SET largest TO score
END IF
END FOR
OUTPUT largest
A flowchart uses standard shapes and arrows to show the path through a process. A start or end is often shown as a terminator, an action as a process box, and a question as a decision diamond with labelled branches. Flowcharts are useful for seeing where a process splits, repeats or stops.
For a small calculation, pseudocode may be quicker. For a process with several choices, a flowchart can reveal missing paths. Neither format guarantees that the algorithm is correct; the logic still needs to be checked against the problem.
How algorithms solve searching problems
A search algorithm looks for a target item in a collection. The simplest method is linear search: examine items one by one until the target is found or the collection ends. It works even when the items are not sorted, but may need to inspect every item.
Binary search is faster on a sorted collection. It checks the middle item and then discards the half that cannot contain the target. It repeats this process on the remaining half. The sorted-data requirement is essential; applying binary search to an unsorted list can produce an incorrect answer.
Linear search: check items one at a time.
Binary search: check the middle of sorted data,
then repeatedly keep the half that may contain the target.
How algorithms sort information
Sorting algorithms arrange data according to a rule, such as alphabetical order or increasing numerical value. Sorting is useful because ordered data can be easier to inspect, compare and search. Different sorting algorithms trade off simplicity, memory use and performance.
A beginner may first learn bubble sort, which repeatedly compares neighbouring items and swaps them when they are in the wrong order. It is easy to visualise but inefficient for large collections. More advanced methods such as merge sort and quicksort are often much faster in practical settings, although their performance depends on the data and implementation.
What is algorithm efficiency?
An algorithm’s efficiency describes the resources it uses, particularly time and memory, as the size of the input grows. Computer scientists use complexity analysis to reason about this growth. Big O notation is commonly used to describe an upper bound on how an algorithm’s resource needs scale, under a stated analysis.
- O(1): constant time in the simplified model; the work does not grow with input size.
- O(log n): logarithmic growth, as in a well-implemented binary search over sorted data.
- O(n): linear growth, as in a full pass through a list.
- O(n log n): common growth for efficient comparison-based sorting methods.
- O(n²): quadratic growth, which can arise in simple nested-loop methods.
Big O does not tell you the exact number of seconds a program will take. Actual performance also depends on hardware, programming language, memory, input structure, implementation details and constant factors. Complexity is a tool for comparing growth, not a stopwatch.
How to test whether an algorithm works
- Use a normal case: test a typical input and check whether the output matches the expected result.
- Use a boundary case: test the smallest permitted input, the largest relevant value or an empty collection if it is allowed.
- Use an unusual case: try repeated values, negative numbers, text with different capitalisation or data already in order.
- Trace the steps: write down how the variables change at each stage.
- Check termination: confirm that each loop makes progress and that the stopping condition can be reached.
- Compare with an independent method: for small examples, solve the problem manually and check the algorithm’s result.
Testing should be linked to the problem specification. A program can run without crashing yet still give a wrong answer. For example, an algorithm may find the largest score but fail when all values are negative if it incorrectly starts the largest value at zero.
Algorithms in everyday life
Navigation apps use algorithms to estimate routes, compare travel times and update recommendations as traffic changes. Online shops use algorithms to rank products, process orders and detect unusual activity. Hospitals use algorithms to support image analysis and scheduling, while scientists use them to process measurements and test models.
An algorithm does not make a decision automatically fair or correct. Its outcome depends on the data, objective, assumptions and rules chosen by people. A route-planning algorithm may optimise time but not accessibility; a recommendation system may show popular items but not the best information for a particular learner. The problem definition matters as much as the method.
Algorithms and artificial intelligence
Algorithms are used throughout artificial intelligence, but the two terms do not mean the same thing. An algorithm is a method for carrying out a task. An AI system may use algorithms to train a model, process inputs or generate outputs. Some AI methods learn patterns from data, while many ordinary algorithms follow rules written directly by developers.
A calculator’s addition procedure is an algorithm, but it is not usually described as artificial intelligence. A machine-learning model uses algorithms during training and operation, but its behaviour is shaped by the model, training data, objective and deployment conditions. Readers should not assume that every algorithm is intelligent or that every AI answer is automatically reliable.
Common algorithm mistakes
- Vague instructions: words such as ‘suitable’, ‘best’ or ‘nearby’ need measurable definitions for a computer to act consistently.
- Missing cases: an algorithm may work for positive values but fail for zero, negative values or empty inputs.
- Wrong order: moving an instruction before the data it depends on can change the result.
- Unbounded loops: a loop without a reliable stopping condition can run indefinitely.
- Confusing correctness and speed: a fast algorithm is not useful if it produces the wrong result.
- Ignoring the data: an algorithm may rely on sorted input, unique identifiers or valid formats; those assumptions must be checked.
Frequently asked questions about algorithms
What is an algorithm in simple words?
An algorithm is a clear, finite set of steps for solving a problem or completing a task.
Is an algorithm the same as code?
No. Code is an implementation in a programming language. The algorithm is the underlying method that the code carries out.
Are algorithms only used by computers?
No. People use algorithms in recipes, calculations, procedures and other repeatable tasks. Computers are especially useful for carrying out them quickly and consistently.
What is the difference between an algorithm and AI?
An algorithm is a method. Artificial intelligence is a field of systems that perform tasks associated with capabilities such as learning, perception or language generation; AI systems use algorithms.
Why do algorithms need testing?
Testing checks whether the steps produce correct results for typical, boundary and unusual inputs.
Which algorithm is fastest?
There is no single fastest algorithm for every task. Efficiency depends on the problem, input size, data structure, hardware and assumptions.
Continue learning at eduKateSG
For the next step in computing, explore Modular and Incremental Algorithm Design. For a practical programming introduction, read What Is Python?. Together, these explain the difference between planning a method and implementing it in code.
Sources and further reading
The Encyclopaedia Britannica introduction to algorithms provides a general definition. The USFCA algorithm visualisations offer interactive demonstrations of common searching and sorting methods. For formal study, consult an introductory computer-science text on algorithm design and analysis. Examples here are instructional and use fictional data.