Input and output
It starts from something and produces an answer.
Free Lesson
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
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.
Checks: 0
Start at the first item and check each one in order.
Press Next to make the first check.
Checks: 0
Check the middle item, then throw away the half that cannot hold the number.
Press Next to make the first check.
Four quick questions about how the two searches grow.
Definition
It starts from something and produces an answer.
Each step is clear enough to follow without guessing.
It stops after a finite number of steps, even when the answer is 'not found'.
It gives the right answer for every valid input, not just the easy ones.
It uses as few steps and as little memory as it can as the input grows.
Examples
Ingredients in, a cake out. The steps are exact, in order, and they end.
Map apps run route-finding algorithms such as Dijkstra's to pick the fastest path.
You open a dictionary near the middle and skip halves: that is binary search.
Sorting a hand of cards or a spreadsheet column uses algorithms like insertion sort or merge sort.
Ranking algorithms order billions of pages so the most useful results come first.
Streaming and shopping sites use algorithms that predict what you will like next.
Compare
| Aspect | Linear search | Binary search |
|---|---|---|
| How it works | Check items one by one from the start | Check the middle, discard half, repeat |
| Needs a sorted list? | No | Yes |
| Worst case on n items | n checks | about log2(n) checks |
| Big-O | O(n) | O(log n) |
| Best for | Short or unsorted lists, searching once | Large sorted lists you search many times |
Most checks each search needs to find a number, or prove it is missing, in a sorted list:
| List size | Linear search | Binary search |
|---|---|---|
| 16 | 16 | 5 |
| 100 | 100 | 7 |
| 1,000 | 1,000 | 10 |
| 1,000,000 | 1,000,000 | 20 |
| 1,000,000,000 | 1,000,000,000 | 30 |
How to
Step 1
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.
Step 2
Break the job into small, exact steps a person or computer could follow without guessing, like 'check the middle item'.
Step 3
Decide what happens with an empty list, a missing target or a duplicate, so every path ends with an answer.
Step 4
Trace a small example by hand, then count the steps as the input grows to see how fast the algorithm scales.
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.
FAQ
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.
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.
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.
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.
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.