COMPUTER SCIENCE EXPLAINED

Sorting algorithms and big O notation, explained

A sorting algorithm puts items in order using simple operations, mostly comparisons and swaps, and big O notation describes how its work grows with the number of items. In Vidirexa, a browser studio, Sort Spectrum runs real sorting algorithms side by side and Algo Arena pits search strategies against each other, recorded as a vertical 9:16 MP4.

This explainer covers the ideas behind every sorting animation: what counts as a step, how to compare algorithms without a stopwatch, why some algorithms are fast on one input and slow on another, and the neat proof that sorting by comparisons has a speed limit. It ends with the two classic ways to search a grid.

Updated 29 November 2026.

What does a sorting algorithm actually do?

A sorting algorithm takes a list and rearranges it into order using a small set of operations. Most algorithms are built from two: comparing two items to see which is smaller, and moving items, often by swapping a pair. Counting those operations is a fair way to compare algorithms, because the count does not depend on how fast the computer is.

Two more properties matter in practice. A sort is in place when it needs only a small, fixed amount of memory beyond the list itself; heap sort is in place, while the usual merge sort needs a second list as long as the first. A sort is stable when items that compare as equal keep their original order, which matters when you sort by one field and then another, for example by city and then by surname.

Sort Spectrum makes these operations visible. Each algorithm runs its own steps on the values, a note sounds for every operation, pitched by the value involved, and a final sweep confirms the order. Its Mode shows the algorithms One after another or as a Battle side by side, with up to eight of its 47 sorts.

What does big O notation mean?

Big O notation describes how the work of an algorithm grows as its input grows, ignoring constant factors and smaller terms. Saying an algorithm is O(n²) means that, for large lists, doubling the length roughly quadruples the work; O(n log n) means that doubling the length slightly more than doubles it. Big O describes the shape of the growth, not an exact count.

The difference becomes enormous as n grows. Bubble sort and insertion sort compare up to n(n − 1) ÷ 2 pairs in the worst case, which is 120 comparisons for 16 items but 523,776 for 1,024 items. Merge sort needs at most n log₂ n − n + 1 comparisons when n is a power of 2: 49 for 16 items and 9,217 for 1,024. At 16 items the gap is small; at 1,024 items the quadratic sort does more than 50 times as much work.

The table compares the two growth rates with the limit explained in the next section. For a sense of scale, n log₂ n for a million items is about 20 million, while n² is a million million.

How n², n log₂ n and the comparison limit grow
Items (n)n² (quadratic)n log₂ nComparison limit, log₂(n!)
1625664about 44.3
25665,5362,048about 1,684
1,0241,048,57610,240about 8,769

Why can no comparison sort beat n log n?

A short argument sets a speed limit for every algorithm that sorts only by comparing pairs. A list of n different items can be arranged in n! ways (n factorial: 1 × 2 × 3 × … × n). The algorithm must tell all of these arrangements apart, because each needs a different sequence of moves to end up sorted. Each comparison has two possible answers, so k comparisons can distinguish at most 2ᵏ cases. Covering n! cases needs 2ᵏ ≥ n!, which means k ≥ log₂(n!).

For 16 items, 16! is 20,922,789,888,000 and log₂(16!) is about 44.25, so any comparison sort needs at least 45 comparisons in its worst case. For large n, log₂(n!) grows like n log₂ n, which is why merge sort, heap sort and the other O(n log n) algorithms are as good as comparison sorting gets, up to a constant factor.

Algorithms that do not compare pairs can escape the limit. Radix sort looks at the digits of each key and sorts by one digit position at a time, so it needs about n × k steps for keys of k digits; for many items with short keys, that beats n log n. Sort Spectrum includes Radix Sort, and its picture changes in passes, one per digit, rather than item by item.

Why does the starting order matter?

Big O usually describes the worst case, but many algorithms behave very differently on different inputs. Insertion sort on a nearly sorted list does close to n comparisons, because every item is already near its place; on a reversed list it does the full n(n − 1) ÷ 2. That makes it a good choice for short or nearly sorted lists, and some practical sorts, such as Timsort, hand short runs to insertion sort.

Quick sort is the opposite case. On average it needs about 1.39 n log₂ n comparisons and it is fast in practice, but with an unlucky choice of pivot, such as always picking the first item of a list that is already sorted, it slows to n². Good implementations pick pivots at random or take the median of a few items to make that unlikely.

Sort Spectrum lets you test this with its Start order setting: Random, Reversed, Nearly sorted, Pipe organ or Few unique. Running the same algorithms on Random and then on Nearly sorted is a quick way to see which ones take advantage of order that is already there, and Few unique shows how an algorithm copes with many equal values.

At the far end sits Bogo sort, which shuffles the list at random and checks whether it happens to be sorted. For n different items, the chance that one shuffle is sorted is 1 in n!, so on average it needs n! shuffles: 3,628,800 for just ten items. It is a joke algorithm, but a good demonstration of how fast factorials grow. Sleep sort is another joke: it starts a timer for each value and outputs the value when its timer runs out.

Which sorting algorithm is the fastest?

There is no single answer, which is why so many algorithms exist. For general data compared item by item, O(n log n) algorithms win on large lists: merge sort keeps that speed even in its worst case and is stable, heap sort does so in place, and quick sort is usually fastest in practice despite its rare bad case. For short lists, simple algorithms such as insertion sort often win because each of their steps is cheaper. For keys in a known small range, such as ages or single digits, counting and radix sorts beat comparison sorts.

Hardware matters too. Reading memory in order is much faster than jumping around, so an algorithm that moves through memory smoothly can beat one that does fewer operations. Parallel sorts such as bitonic sort do more comparisons in total but arrange them so that many happen at once, which suits graphics processors.

  • Large general lists: an O(n log n) sort, and merge sort when equal items must keep their order.
  • Short or nearly sorted lists: insertion sort.
  • Whole numbers in a small range: counting sort or radix sort.
  • Many processors working at once: a sorting network such as bitonic sort.

How do search algorithms such as BFS and DFS differ?

Search algorithms explore a structure, such as a grid, a maze or a network, from a starting point. Breadth-first search (BFS) keeps a queue of places to visit and always takes the oldest one first, so it explores in rings: everything one step away, then everything two steps away, and so on. On a grid it finds the shortest path to every cell it reaches.

Depth-first search (DFS) keeps a stack and always takes the newest place first, so it runs as far as it can in one direction before backing up. It needs little memory and is the basis of many maze generators, but the first path it finds is often long and winding. Both visit every reachable place once, so both take time proportional to the number of places plus the number of connections between them, written O(V + E).

Algo Arena turns search strategies into a territory battle. Each round every team takes one cell touching its own, so the way a team's algorithm chooses among the cells it touches decides the shape of its territory. Teams can use BFS, DFS, RANDOM, EXPLORER, PRISM, RICOCHET, DRAGON, ARCS or LIGHTNING; when nobody can move, the smallest team is removed and its cells reopen, until one algorithm is left.

How can I build an intuition for algorithms?

Run small cases by hand. Sort eight playing cards with insertion sort and count your comparisons, then shuffle and try merge sort. Predict before you run: on a reversed list, which one does more work? Then check your prediction against a visualization, where you can see every comparison and swap and hear the rhythm each algorithm makes.

A good first experiment: open Sort Spectrum in Battle mode with Insertion, Merge and Quick Sort, run it on Random and then on Nearly sorted, and compare the finishing order.

Games for this guide

  • Sort Spectrum preview frameSort Spectrum: Up to eight of 47 sorting algorithms rebuild a rainbow on shapes like a wheel or galaxy, a note per step.
  • Algo Arena preview frameAlgo Arena PRO: Search algorithms battle for territory on a carved arena. Each round every contestant takes one cell touching its own, a stuck team may capture a dominated enemy cell, and when nobody can move the smallest team is removed. Last algorithm standing wins.

Video makers for this

Questions

What is the fastest sorting algorithm?

It depends on the data. For large general lists, O(n log n) algorithms such as merge sort, heap sort and quick sort are the standard choices; for short or nearly sorted lists insertion sort is often faster; for keys in a small range, radix or counting sort can beat them all.

What does O(n log n) mean?

That the work grows a little faster than the number of items: doubling the list slightly more than doubles the work. It is the best possible growth for sorting by comparisons alone.

Why is bubble sort still taught?

Because it is easy to understand and to prove correct, which makes it a good first example. Its O(n²) comparisons make it a poor choice for long lists.

What is a stable sort?

One that keeps items with equal keys in their original order. It matters when you sort the same list by several fields, one after another.

What is the difference between BFS and DFS?

Breadth-first search explores everything one step away before going further and finds shortest paths on a grid. Depth-first search follows one branch as far as it goes before backing up and needs less memory.

More guides

All creator guides · Start creating