What is an Algorithm?
An algorithm is a step-by-step set of instructions to solve a problem.
That's it. No magic. An algorithm is just a recipe — a clearly defined sequence of steps that takes an input, does something with it, and produces an output.
You follow algorithms every day. A recipe tells you how to make pasta. A GPS route tells you how to get home. The boarding procedure at an airport is an algorithm. They're all finite sets of steps that solve a specific problem.
In computing, an algorithm tells the computer exactly what to do with data.
The Four Properties of a Good Algorithm
Not every set of steps qualifies as a useful algorithm. A good one has four properties:
1. Correctness — It produces the right output for every valid input. An algorithm that works 99% of the time isn't correct.
2. Finiteness — It terminates. It can't run forever.
3. Efficiency — It uses a reasonable amount of time and memory. We'll measure this precisely with Big O notation.
4. Clarity — It can be understood, implemented, and debugged by others.
Algorithms vs Programs
An algorithm is a concept. A program is one implementation of that concept in a specific language.
The same sorting algorithm (say, bubble sort) can be written in Python, C++, Go, or Java. The language changes. The algorithm stays the same.
Think of an algorithm like a blueprint. The blueprint for a house describes what to build. The actual construction — with specific materials, workers, and tools — is the implementation.
A Concrete Example: Finding the Maximum
Problem: Given a list of numbers, find the largest one.
Algorithm:
- Start with the first element as the current maximum.
- Go through each remaining element.
- If the current element is larger than the current maximum, update the maximum.
- Return the maximum.
That's the algorithm. Now the implementation:
The algorithm is identical across all three. Only the syntax differs.
Algorithms and Data Structures Work Together
Algorithms don't work in a vacuum — they operate on data structures. The same problem can be solved with different algorithm + data structure combinations, each with different trade-offs.
Example: Checking if a number appears in a list
| Approach | Data Structure | Algorithm | Time |
|---|---|---|---|
| Scan every element | Array | Linear search | O(n) |
| Jump to middle, divide | Sorted array | Binary search | O(log n) |
| Hash the key | Hash map | Direct lookup | O(1) average |
Same problem. Three different combinations. Wildly different performance.
This is why you can't learn algorithms without learning data structures — and vice versa. They're two halves of the same skill.
Common Algorithm Families
As you go through this roadmap, you'll encounter these families over and over:
| Family | Idea | Examples |
|---|---|---|
| Search | Find an element in a collection | Linear search, Binary search |
| Sort | Arrange elements in order | Bubble, Merge, Quick sort |
| Divide & Conquer | Break problem into sub-problems | Merge sort, Binary search |
| Greedy | Always pick the locally best option | Activity selection, Huffman |
| Dynamic Programming | Cache sub-problem results | Fibonacci memo, Knapsack |
| Backtracking | Explore all possibilities, prune dead ends | N-Queens, Sudoku solver |
| Graph traversal | Visit nodes in a graph | BFS, DFS, Dijkstra |
Each family has a strategy. Once you know the strategy, you can recognise which problems it applies to — even problems you've never seen before. That pattern recognition is what DSA practice actually builds.