Skip to main content
Be Bitwise

What Is an Algorithm?

Algorithms & Complexity · Coding Fundamentals track

5 min read

In this module

A Precise Set of Steps

An algorithm is a finite, unambiguous sequence of steps that solves a specific problem. The formal definition is dry, but the intuition is easy: it's a recipe. Feed it input, follow the steps, and out comes the right answer.

You run algorithms every day without thinking about them. Finding a word in a dictionary. Sorting a deck of cards. Working out the fastest route to work. In computing, we formalise them so a machine can execute them precisely and repeatedly.

What Makes a Good Algorithm?

Not all solutions are created equal. Two algorithms can solve the same problem yet wildly diverge in how much time and memory they burn through.

Consider finding a name in a phone book:

Algorithm A (linear search): Start at page one. Check every name, one by one, until you find the right one.

Algorithm B (binary search): Open to the middle. Is the name before or after this page? Discard the wrong half. Repeat with the remaining half.

For a phone book with 1,000,000 names, Algorithm A might check up to 1,000,000 entries. Algorithm B needs at most 20 checks, because log2(1,000,000) is about 20. Same problem, wildly different performance. If each check takes one microsecond, Algorithm A could chew through a full second while B is done in twenty. A 50,000x gap.

The disparity yawns wider as inputs grow. For a billion names, Algorithm A needs a billion checks. Algorithm B needs 30. Picking the right algorithm can be the difference between a program that runs in milliseconds and one that runs for hours.

You can watch a small version of the race below. The stepper holds 15 sorted numbers and is looking for 94. Press play with linear search selected and count the checks as it walks from the left: it needs 13. Then switch the drop-down to binary search and run it again. It finds 94 in 4, because each check throws away half of what's left.

Algorithm Visualizer

258121623384556728190949799
Linear search for 94
300ms
Comparing
Swapping
Sorted
Pointer
Comparisons: 0Step: 1/15

Thirteen against four doesn't sound like much. Run the same comparison on a million names and it's a million against twenty.

Key Concept

An algorithm is a finite sequence of well-defined steps that turns input into output. The study of algorithms is about finding an efficient solution, measuring how the resources required (time and space) scale as the input grows.

Correctness First, Then Efficiency

An algorithm must be correct. It must produce the right answer for every valid input. A blazing fast algorithm that gives wrong answers is worse than useless.

You can prove correctness informally (reasoning about why each step works) or formally (mathematical induction, loop invariants). For our purposes the takeaway is: before you worry about speed, make sure the algorithm actually works. An incorrect algorithm is broken, however fast it runs.

Once you have a correct algorithm, the natural follow-up is whether you can make it faster. This is where algorithm design becomes both an art and a science. Sometimes a small shift in approach yields dramatic speedups. Other times, no amount of cleverness can beat a fundamental limit.

Expressing Algorithms

Algorithms can be expressed in many ways:

Natural language: "Start at the first element. Compare it with every other element. If you find a match, return its position."

Pseudocode: A structured but language-independent notation.

text
function find(list, target):
for i = 0 to length(list) - 1:
if list[i] == target:
return i
return -1 // not found

Flowcharts: Visual diagrams with decision points and arrows.

Code: An implementation in an actual programming language.

We'll lean on pseudocode here because it puts the logic up front without dragging in a particular language's syntax. You think about the algorithm rather than chasing semicolons and brackets.

Deterministic vs. Non-Deterministic

Most algorithms are deterministic. Same input, same output, same steps every time. Others lean on randomness. A randomized algorithm makes random choices as it runs, so the same input can take different paths through the code. Quicksort with random pivot selection is a classic case. The sorted result is always identical, but the internal trail varies.

Randomized algorithms aren't just academic novelties. Many of the fastest known algorithms for important problems are randomized. The dice rolls help dodge the worst-case inputs that haunt deterministic versions.

The Importance of Algorithmic Thinking

Learning algorithms is mostly about developing algorithmic thinking: the ability to break down a problem, identify the right approach, and reason about efficiency.

When a new problem lands on your desk, algorithmic thinking walks you through:

  1. What is the input and desired output?
  2. What are the constraints?
  3. Can I reduce this to a known problem?
  4. How does the solution scale as the input grows?
  5. Can I do better?

This analytical lens reaches well beyond coding interviews. It's how you reason about system design, tune database queries, pick data structures, and work out why some programs are fast while others crawl.

Algorithms and Hardware

Algorithms don't exist in a vacuum. They run on real hardware. Theoretical analysis tells you how an algorithm scales, but real-world performance hinges on cache behaviour, branch prediction, memory access patterns, and parallelism. An algorithm with worse theoretical complexity sometimes wins simply because it's friendlier to the hardware.

A linear search through a small array can outpace binary search, for instance: the array fits in a cache line or two, and its loop branch is easy to predict, while binary search's go-left-or-right branch is close to a coin toss that the CPU's branch predictor keeps getting wrong. The theoretically superior algorithm loses because at small sizes the constant factors in its operation drown the asymptotic advantage.

Understanding both the abstract analysis and the physical reality is what separates a competent programmer from a great one. The next lesson formalises the tools for measuring algorithmic efficiency with Big O notation.