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:

  1. Start with the first element as the current maximum.
  2. Go through each remaining element.
  3. If the current element is larger than the current maximum, update the maximum.
  4. Return the maximum.

That's the algorithm. Now the implementation:

Loading...

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

ApproachData StructureAlgorithmTime
Scan every elementArrayLinear searchO(n)
Jump to middle, divideSorted arrayBinary searchO(log n)
Hash the keyHash mapDirect lookupO(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:

FamilyIdeaExamples
SearchFind an element in a collectionLinear search, Binary search
SortArrange elements in orderBubble, Merge, Quick sort
Divide & ConquerBreak problem into sub-problemsMerge sort, Binary search
GreedyAlways pick the locally best optionActivity selection, Huffman
Dynamic ProgrammingCache sub-problem resultsFibonacci memo, Knapsack
BacktrackingExplore all possibilities, prune dead endsN-Queens, Sudoku solver
Graph traversalVisit nodes in a graphBFS, 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.