Free Lesson

What Is an Algorithm?

An algorithm is a clear, step-by-step set of instructions for solving a problem, like a recipe a computer can follow. Different algorithms can solve the same problem, and some need far fewer steps than others.

See it for yourself below: two search algorithms look for the same number, and you count how many checks each one needs. Everything runs in your browser.

Interactive lab

Find the number: linear vs binary search

Both algorithms hunt for the same hidden number in the same 16-item list. Each press of Next makes one check in each search. Crossed-out numbers are ruled out.

List order

Linear search

Checks: 0

Start at the first item and check each one in order.

  1. 3, still possible
  2. 7, still possible
  3. 12, still possible
  4. 18, still possible
  5. 21, still possible
  6. 26, still possible
  7. 30, still possible
  8. 34, still possible
  9. 41, still possible
  10. 47, still possible
  11. 52, still possible
  12. 58, still possible
  13. 63, still possible
  14. 69, still possible
  15. 75, still possible
  16. 82, still possible

Press Next to make the first check.

Binary search

Checks: 0

Check the middle item, then throw away the half that cannot hold the number.

  1. 3, still possible
  2. 7, still possible
  3. 12, still possible
  4. 18, still possible
  5. 21, still possible
  6. 26, still possible
  7. 30, still possible
  8. 34, still possible
  9. 41, still possible
  10. 47, still possible
  11. 52, still possible
  12. 58, still possible
  13. 63, still possible
  14. 69, still possible
  15. 75, still possible
  16. 82, still possible

Press Next to make the first check.

Predict before you peek

Four quick questions about how the two searches grow.

  1. A sorted list has 1,000 names. At most how many checks does binary search need?
  2. You double the list from 1,000 to 2,000 items. What happens to linear search's worst case?
  3. Same doubling, 1,000 to 2,000. What happens to binary search's worst case?
  4. The list is shuffled, not sorted. Which search is still guaranteed to work?

Definition

What makes something an algorithm

Input and output

It starts from something and produces an answer.

Exact steps

Each step is clear enough to follow without guessing.

It finishes

It stops after a finite number of steps, even when the answer is 'not found'.

Correct

It gives the right answer for every valid input, not just the easy ones.

Efficient

It uses as few steps and as little memory as it can as the input grows.

Examples

Algorithm examples from everyday life

A recipe

Ingredients in, a cake out. The steps are exact, in order, and they end.

Driving directions

Map apps run route-finding algorithms such as Dijkstra's to pick the fastest path.

Looking up a word

You open a dictionary near the middle and skip halves: that is binary search.

Sorting

Sorting a hand of cards or a spreadsheet column uses algorithms like insertion sort or merge sort.

Search engines

Ranking algorithms order billions of pages so the most useful results come first.

Recommendations

Streaming and shopping sites use algorithms that predict what you will like next.

Compare

Linear search vs binary search

AspectLinear searchBinary search
How it worksCheck items one by one from the startCheck the middle, discard half, repeat
Needs a sorted list?NoYes
Worst case on n itemsn checksabout log2(n) checks
Big-OO(n)O(log n)
Best forShort or unsorted lists, searching onceLarge sorted lists you search many times

Why the gap grows

Most checks each search needs to find a number, or prove it is missing, in a sorted list:

List sizeLinear searchBinary search
16165
1001007
1,0001,00010
1,000,0001,000,00020
1,000,000,0001,000,000,00030

How to

How to write an algorithm

  1. Step 1

    Define input and output

    Write down what you start with and what the answer looks like, such as a sorted list and a target number in, a position or 'not found' out.

  2. Step 2

    List the steps

    Break the job into small, exact steps a person or computer could follow without guessing, like 'check the middle item'.

  3. Step 3

    Handle the edge cases

    Decide what happens with an empty list, a missing target or a duplicate, so every path ends with an answer.

  4. Step 4

    Test and measure

    Trace a small example by hand, then count the steps as the input grows to see how fast the algorithm scales.

Learning to code with AI?

Code Card turns your Claude Code, Codex, and OpenClaw sessions into a public developer profile with activity graphs, token breakdowns, badges, and a shareable card. Start with npx code-card.

Make your Code Card

FAQ

Algorithm questions, answered

What is an algorithm in simple words?

An algorithm is a clear, step-by-step set of instructions for solving a problem or finishing a task. A recipe, driving directions and the steps a computer follows to search a list are all algorithms.

What are some examples of algorithms?

Everyday examples include a recipe, long division and the steps for tying a shoe. In computing, common algorithms include linear search, binary search, sorting algorithms like merge sort, GPS route finding and the ranking a search engine uses to order results.

What is the difference between linear search and binary search?

Linear search checks items one at a time from the start, so it works on any list but may check every item. Binary search checks the middle of a sorted list and throws away half each time, so it needs about log2(n) checks, around 20 for a million items, but it only works when the list is sorted.

Why does binary search need a sorted list?

Binary search decides which half to throw away by comparing the target with the middle item. That only makes sense if every item to the left is smaller and every item to the right is bigger. On an unsorted list it can discard the half that holds the target and wrongly report it missing.

Is an algorithm the same as a program?

No. An algorithm is the idea, the steps themselves. A program is an algorithm written in a programming language such as Python or JavaScript so a computer can run it. The same algorithm can be written as many different programs.