What Is an Algorithm? Search, Sorting and Big-O Explained

15 min read · Updated: October 23, 2026

An algorithm is a set of steps for solving a problem. It always comes to an end, and it gives the same result no matter who follows it. Two sets of steps can reach the same answer with very different amounts of work. To find one item among 1 million sorted items, checking from one end can take up to 1 million comparisons. Cutting the range in half each time takes only 20.

What you'll learn

  • What an algorithm is, and how it is like (and unlike) a cooking recipe
  • Why different steps for "searching" and "sorting" can differ in speed by tens of thousands of times
  • How to read time complexity (Big-O notation): what O(n) and O(n²) mean
  • Why a faster computer cannot rescue a roundabout method, and how steps are reworked for GPUs

Prerequisites: none. If you know multiplication and the ideas of "doubling" and "halving," you can follow along.

In everyday life

Type a destination into a map app, and a route appears in seconds. Type words into a search engine, and useful pages rise to the top out of a huge number of pages. A video app suggests videos you are likely to watch.

Now picture putting a shuffled deck of playing cards in number order. You might keep finding the smallest card and pulling it out. Or you might split the cards into piles and then combine them. People always pick some method, and the time it takes depends a lot on the method.

Steps like these, which lead to an answer, are called an algorithm. In the news, you often see phrases like "the social media algorithm." This article explains what an algorithm is and checks, with numbers, why better steps make computing faster.

An algorithm is a clear set of steps that ends

An algorithm writes out, in order, the stages for solving a problem. It takes in the ingredients (the input), goes through fixed stages and produces an answer (the output). For example, here is how to find the largest number in a list. Treat the first number as the "largest so far." Compare it with the next number, and if the next one is bigger, it becomes the new largest so far. Repeat this until the last number.

Two conditions set an algorithm apart from just "a way of doing things." First, every stage has a clear meaning, so anyone who follows it does the same thing. Second, for any input, it always finishes at some point. The computer scientist Donald Knuth listed five properties of an algorithm in his famous textbook. They are these two, plus input, output and being possible to actually carry out.

The CPU, the central part of a computer, simply carries out simple instructions like "add" and "compare," one at a time. An algorithm is the idea of which instructions to line up, in which order, to get the answer. "What Is a CPU?" explains how a CPU works.

The word "algorithm" comes from the name of al-Khwarizmi, a mathematician of the 9th century. He wrote a book on calculating with Indian numerals, the digits we use today. It was translated into Latin and spread across Europe. The form of his name used in that translation became the source of the word.

Two ways to search: one by one, or by halves

Say you need to find the person with a certain ID number in a list of 1 million members. The most obvious way is to compare the entries one at a time, starting from the top. This method is called "linear search." If you are unlucky, you make 1 million comparisons, right up to the last entry.

If the list is sorted by number, there is a better way. First, compare with the entry in the middle. If the number you want is bigger, it cannot be in the first half, so you keep only the second half. Repeat this, and each comparison cuts the range in half. This method is called binary search.

Binary search finds 52 among 15 sorted cards in 4 steps Find 52 (cards sorted smallest first) Step 1: bigger than 35 → go right 3 8 12 17 21 26 30 35 41 47 52 58 63 70 76 Step 2: smaller than 58 → go left 41 47 52 58 63 70 76 Step 3: bigger than 47 → go right 41 47 52 Step 4: equals 52 → found 52
The thick outline marks the middle card compared in that step. The dotted areas are ranges we now know we can skip. Each comparison leaves about half as many cards as before. Left out of this figure: the rule for which of the two middle cards to pick when an even number of cards remain.

Keep halving 1 million entries, and you get 500,000, then 250,000, and so on. By the 20th comparison, the search is always settled. That is because multiplying 2 by itself 20 times gives 1,048,576, which is more than 1 million. So binary search finishes in at most 20 comparisons, whether or not the number is in the list. 1 million comparisons versus 20 is a 50,000-fold difference.

When people look up a word in a paper dictionary, they do not turn the pages one by one from the start. They open it at a rough guess, see whether the word is before or after, and narrow it down. Binary search repeats this narrowing mechanically, always at the exact middle.

Binary search does have one condition: the data must already be sorted. In a shuffled list, comparing with the middle tells you nothing about which side the target is on. That is why sorting, which we look at next, is also an important job.

Sorting can also differ by tens of thousands of times

Putting data into a set order is called "sorting." Here we compare two methods. As a measure of the work, we count how many times two cards are compared to see which is bigger.

The first is the obvious method. Among the cards not yet placed, find the smallest one and add it to the end of the row. To find the smallest, you have to look at all the remaining cards. With n cards, the first round takes n−1 comparisons, the second takes n−2, and so on, one fewer each time. The total is about n×n÷2 comparisons. (This method is called selection sort.)

The second is "merge sort." Split the pile of cards in half, then split each half again, until every pile has one card. Next, combine the small piles two at a time. Compare the top cards of the two piles and take the smaller one. Repeat this, and you get one sorted pile.

Merge sort is fast because it takes only a few rounds of halving. Even 1 million cards become single cards after about 20 halvings. In each round, combining only looks at every card once. So the number of comparisons is roughly "number of cards × number of halvings." The number of times you halve n to reach 1 is written log₂n, so this is n×log₂n comparisons.

Number of cardsObvious method (about n×n÷2)Merge sort (about n×log₂n)Difference
52 (one deck of cards)1,326About 300About 4 times
10,000About 50 millionAbout 130,000About 380 times
1 millionAbout 500 billionAbout 20 millionAbout 25,000 times

The merge sort column is an estimate from n×log₂n, and the actual number of comparisons is a little lower. For one deck, the difference is only about 4 times, so either method is fine when sorting by hand. But the gap grows wider and wider as the number of cards grows. If you want to watch them in action, the sorting algorithm visualizer lets you compare several methods.

Time complexity: compare how the work grows

What matters in these examples is not the amount of work but how it grows. When the data doubles, the work for linear search doubles. Binary search needs just one more step. The obvious sorting method needs four times as much.

How the work grows as the amount of data grows is called time complexity. It is a yardstick for comparing different steps that solve the same problem, regardless of how fast the computer is.

Time complexity is written with an "O," as in O(n) or O(n²). This way of writing is called Big-O notation, and the n inside the brackets is the amount of data. O(n²) means "the work grows roughly in proportion to n×n." Constant factors like "÷2" are dropped. They do not change how many times more work you need when the data doubles.

NotationWhen n doublesRough count at n = 1 millionExample
O(1)No change1Reading an item whose position you know
O(log n)One more stepAbout 20Binary search
O(n)2 times1 millionLinear search, adding up a total
O(n log n)A little over 2 timesAbout 20 millionMerge sort
O(n²)4 times1 trillionThe obvious sort, comparing every pair
O(2ⁿ)2 times when n grows by just 1(About 1.1 quadrillion even at n = 50)Trying every combination

The log in this table is counted the same way as before: how many halvings it takes to reach 1. The O(n²) row says 1 trillion, not the 500 billion in the earlier table. That is because the factor "÷2" is dropped.

The most troublesome one is O(2ⁿ). Suppose you try every way of choosing which of 50 items to pick. That is 2 multiplied by itself 50 times, about 1.1 quadrillion (1,100 trillion) ways. Even checking 1 billion ways per second, it takes about 13 days. With 51 items, it doubles again. To try different numbers, the time complexity (Big-O) comparison tool calculates the counts and times.

A faster computer cannot rescue a roundabout method

Picture a computer that can make 1 billion comparisons per second. To keep things simple, assume every comparison takes the same time. Sorting 1 million items with the obvious method takes about 500 billion comparisons. That is about 500 seconds, a little over 8 minutes. Merge sort needs about 20 million comparisons, which takes 0.02 seconds.

So what if you buy a computer that is 100 times faster? Even the obvious method then takes 5 seconds. Yet merge sort on the old computer is still 250 times faster.

Next, say the data grows tenfold, to 10 million items. The work for the obvious method grows 100 times, so even the 100-times-faster computer takes about 500 seconds. Merge sort on the old computer takes about 0.23 seconds. With an O(n²) method, just ten times more data uses up all of a 100-fold speedup.

Thanks to progress in semiconductors, computers have kept getting faster for decades ("What Is Moore's Law?"). This is why research on algorithms is still essential.

Algorithms that made history

Dijkstra's algorithm (1959): finding the shortest route

Suppose you know a set of places and the length of each road between them. This method finds the shortest route from a starting point to every place. It locks in the "shortest distance so far" for places in order of how close they are to the start. Then it updates the distances of the places beyond them. Edsger Dijkstra of the Netherlands designed it in 1956 and published it in a paper in 1959. This method is called Dijkstra's algorithm.

Dijkstra later said he worked it out at a café in Amsterdam in about 20 minutes, without pencil and paper. OSPF, a system that chooses the routes for data across networks, also uses this method to calculate routes.

PageRank (1998): judging a page's importance from its links

Sergey Brin and Larry Page came up with it as graduate students at Stanford University. They presented it in a 1998 paper, together with a prototype of the Google search engine. The idea is that a page is more important if many pages link to it, especially important pages. According to Google's official guide, today's search ranks results by combining many systems, including PageRank.

RSA (1977): using "hard work" as protection

Rivest, Shamir and Adleman of MIT reported it in 1977, and it appeared in a journal in 1978. Multiplying two large prime numbers is easy. But finding the original two primes from the result takes a very long time with any known method. RSA builds its security on this difference in effort. "What Is Encryption?" explains more.

Maze search (1961): routing the wires on a chip

In 1961, C. Y. Lee published a method for finding a path that connects two points while avoiding obstacles. Starting from the start square, it numbers the neighboring squares, then their neighbors, spreading out like a wave. When the numbers reach the goal, it traces them back to the start. Design software that decides the wiring on circuit boards and semiconductor chips automatically has long used methods built on this idea.

Algorithms for chips that work in parallel

It also matters to choose an algorithm that suits the hardware it runs on. A GPU is a chip with thousands or more simple calculation circuits that all run at the same time. To use that power, the steps must be reorganized so they can move forward at the same time.

As an example, take the sum of 1,024 numbers. Adding them one by one takes 1,023 additions, done one after another, each waiting for the previous answer. To do it in parallel, first pair up the numbers and do 512 additions at once. Adding the remaining 512 numbers in pairs again leaves 256. Repeat this, and after the 10th round you have a single total.

Adding 8 numbers in pairs at the same time gives the total in the time of 3 additions Values Step 1 Step 2 Step 3 3 1 4 1 5 9 2 6 4 5 14 8 9 22 31
Additions in the same row do not wait for each other's answers, so they can run at the same time. There are 7 additions in all, but the waiting time is only that of 3. Adding one at a time would take the time of 7. Left out of this figure: how to split the work when there are too few calculation circuits, and the time to fetch the numbers from memory.

Both ways use the same 1,023 additions. What changes is how many times you wait for an earlier answer. Adding in order means waiting 1,023 times, while the parallel way waits only 10 times. A technical document from NVIDIA, a GPU maker, also presents this tree-shaped adding as the basic pattern for summing many numbers on a GPU.

On the other hand, steps that cannot move on without the previous result are hard to run in parallel. Binary search is a typical example, because the result of each comparison decides where to look next. "Why Are GPUs Used for AI?" explains why AI calculations suit this kind of side-by-side work.

Common misconceptions

Myth: An algorithm is a program written in a particular programming language

In fact, an algorithm is the steps themselves, and it does not depend on how they are written down. Binary search is the same binary search whether you write it in Python, in C or do it with pencil and paper. A program is an algorithm written in some language. A compiler or similar tool turns it into machine code, and the CPU runs it ("How Compilers Work"). An algorithm can also be built directly as a circuit. The steps of adding numbers by hand on paper exist inside a CPU as an adder circuit ("How Do Computers Use Binary?"). To run on a computer, an algorithm always takes the form of a program, so the two are easy to confuse.

Myth: With a new, fast computer, it doesn't matter how good the steps are

In fact, with lots of data, an O(n²) method loses to an O(n log n) method even on a computer 100 times faster. While the data is small, the difference does not show. So people tend to notice the problem only after the data has grown. On the other hand, when the data is very small, a method with simpler steps can be faster. Time complexity describes how work grows, and the factors it drops start to matter.

Myth: A social media "algorithm" is one fixed formula

In fact, the word refers to a whole system that combines many sets of steps and AI models. Adam Mosseri, the head of Instagram, explained this in 2021. There is no single algorithm that decides what people see; instead, many systems with different purposes are used. The recommendation system that X (formerly Twitter) released in 2023 is also split into several stages. These include gathering candidates, scoring them with AI and filtering them with rules. The AI models inside are not written line by line by people. They are tuned automatically from data ("What Is Machine Learning?").

By the numbers

ItemNumberNote
Linear search among 1 million itemsUp to 1 million comparisonsCompare one by one from the start
Binary search among 1 million itemsUp to 20 comparisons2 multiplied by itself 20 times is 1,048,576
Sorting 1 million items with the obvious methodAbout 500 billion comparisonsAbout 8 minutes at 1 billion comparisons per second
Sorting 1 million items with merge sortAbout 20 million comparisonsAbout 0.02 seconds at the same speed
Trying every way to choose from 50 itemsAbout 1.1 quadrillion waysAbout 13 days at 1 billion ways per second
Summing 1,024 numbers (in parallel)The time of 10 additions1,023 when adding in order
Dijkstra's algorithmPublished in 1959Designed in 1956
RSAReported in 1977Appeared in a journal in 1978
PageRank1998 paperBrin and Page

The counts above are estimates that assume each comparison takes the same time. Real times also depend on things like how fast data can be brought in from memory.

Learn more

Read next

Articles in the series “How computers work”

  1. How Do Computers Use Binary? Calculating with 0 and 1
  2. What Is a CPU? What It Does Inside Your Computer and Phone
  3. Why Are GPUs Used for AI? Matrix Math and Memory Explained
  4. What Is an Operating System? What an OS Actually Does
  5. How Does CPU Cache Work? Hit Rate, Lines and Locality
  6. What Is an NPU? The AI Engine in Phones and PCs Explained
  7. What Is an FPGA? A Chip You Can Rewire After It's Made
  8. What Is an ASIC? Custom Chips vs CPUs and FPGAs Explained
  9. How Compilers Work: From Source Code to Machine Code
  10. What Is a Logic Gate? AND, OR, NOT and How Gates Compute
  11. What Is RISC-V? The Open Instruction Set Explained
  12. What Is an Algorithm? Search, Sorting and Big-O Explained
  13. What Is a Database? Tables, SQL and Indexes Explained
  14. What Is Git? Version Control Basics and Git vs GitHub

Questions on this topic: Data Structures and Algorithms

Sources

All explainers