What Is an Algorithm?
An algorithm is a precise, step-by-step procedure for solving a problem or completing a task. It takes an input, follows a defined sequence of steps, and produces an output.
Think of a recipe: given ingredients (input), follow the steps, and you get a dish (output). A GPS navigation system is an algorithm too: given a starting point and destination (input), it computes a route (output).
What Makes a Good Algorithm?
- Correct — it produces the right answer for all valid inputs.
- Finite — it terminates and does not loop forever.
- Precise — each step is unambiguous; no room for interpretation.
- Efficient — it uses a reasonable amount of time and memory.
Pseudocode
Pseudocode describes algorithms in a human-readable format, without worrying about syntax of any particular programming language. It uses conventions like if, for, while, and assignment (=).
Algorithm: LinearSearch(array, target)
for i = 0 to length(array) - 1
if array[i] == target
return i
return -1Examples of Algorithms
| Problem | Algorithm | Time Complexity |
|---|---|---|
| Find an item in a list | Linear search | O(n) |
| Find an item in a sorted list | Binary search | O(log n) |
| Sort a list | Merge sort | O(n log n) |
| Find the shortest path | Dijkstra's | O((V + E) log V) |
See how a linear search executes step by step.
The Cost of an Algorithm
Every algorithm takes time and memory. We measure this with Big-O notation, which describes how the cost grows as the input size increases:
- O(1) — constant time, regardless of input size
- O(log n) — grows slowly; doubles in input only adds one step
- O(n) — grows linearly; doubles in input doubles the steps
- O(n²) — grows quadratically; doubles in input quadruples the steps
Read more about complexity analysis.
Try It Yourself
Implement binary search puts these ideas into code.
This article is the first step in the Algorithms Foundations learning path.