Sorting Algorithm Visualizations

Sorting algorithm visualizations turn an abstract instruction — put these numbers in order — into work you can watch. This collection animates eight sorting algorithms step by step: bubble, selection, insertion, merge, quick, and heap sort learn by comparing values; counting sort counts keys; radix sort routes numbers through stable digit buckets. Each one is synced to its real code, running complexity, and the exact moves it performs on the array you give it.

Sorting is where you learn to read algorithms, and the reason is almost unfair: every sort here solves the identical problem, so the only thing that changes from one to the next is strategy. Line them up and the entire vocabulary of algorithm analysis — stable, in-place, adaptive, worst-case — falls straight out of the differences. Learn to see those differences and you are no longer memorizing eight procedures; you are reading eight answers to one question.

How to Read a Sorting Algorithm

Open any visualization, change the input, press play, and watch four things. They are the four questions that separate every sort from every other:

  • How does it decide order? Bubble sort compares neighbors; selection sort scans for a global minimum; merge sort compares only across already-sorted halves. Counting sort and radix sort skip pairwise comparisons entirely: one turns keys into addresses, the other uses stable digit buckets. The order-finding mechanism is the algorithm.
  • Is it stable? When two values are equal, does their original order survive? A stable sort keeps 12a before 12b; an unstable one may flip them. It sounds pedantic until you sort records by a second key.
  • Is it in-place? Does it rearrange the array using O(1) extra room, or does it rent a second array the size of the first? Merge sort buys its guarantee with memory; heap sort gets the same time bound for free.
  • What is its worst case? The average case flatters everyone. The worst case is where quick sort quietly degrades to O(n²) and merge sort does not — and that gap is the whole reason both exist.

Sorting Algorithm Complexity Comparison

The single most useful line in this table is the split through the comparison sorts: three cost O(n²), three cost O(n log n), and that boundary is the difference between sorting a thousand items and sorting a billion. The last two rows break the pattern on purpose — no comparisons at all, bounds written in terms of the keys instead of just n, and a price paid in memory and applicability rather than time.

AlgorithmBestAverageWorstSpaceStable
Bubble sortO(n)*O(n²)O(n²)O(1)Yes
Selection sortO(n²)O(n²)O(n²)O(1)No
Insertion sortO(n)O(n²)O(n²)O(1)Yes
Merge sortO(n log n)O(n log n)O(n log n)O(n)Yes
Quick sortO(n log n)O(n log n)O(n²)O(log n)‡No
Heap sortO(n log n)§O(n log n)O(n log n)O(1)No
Counting sortO(n + k)†O(n + k)O(n + k)O(n + k)Yes
Radix sortO(d·n)†O(d·n)O(d·n)O(n + b)Yes

* Bubble and insertion sort reach O(n) on already-sorted input only because they can notice they are done and stop early — that is what "adaptive" means, and selection sort, which always scans the full remainder, cannot do it.

† k is the key range and d the digit count of the largest key — the non-comparison sorts only beat O(n log n) while those stay small, which is precisely the fine print each lesson puts on screen.

‡ Quick sort's O(log n) is the average recursion depth; partitions that stay lopsided push the stack to O(n).

§ Heap sort's row is an upper bound. A sift stops as soon as the parent is no smaller than its children, so not every input does the same work — an all-equal array finishes in linear time.

A Recommended Learning Order

Start with bubble sort. Nobody ships it, and that is exactly why it goes first: it is the smallest sort that still teaches the quadratic cost of only ever swapping neighbors. Then selection sort, which makes the opposite trade — far fewer writes, but it pays full O(n²) on every input because it refuses to notice an array is already sorted. Insertion sort is the one your standard library actually uses on small arrays; it is the first sort here that is genuinely fast on nearly-sorted data, and watching why is the cleanest introduction to adaptivity.

With the quadratic three understood, the O(n log n) sorts land harder. Merge sort is divide-and-conquer in its purest form: split until sorting is trivial, then do all the work on the way back up, and pay for the guarantee with an O(n) buffer. Quick sort is the partition-based answer that is usually faster than merge sort in practice while having a worse worst case — the most important trade-off in the whole family. Then heap sort, which reaches O(n log n) in place by leaning on a data structure rather than recursion, and ties the sorting unit back to heaps.

The last two sorts break the one rule everything above obeyed: they never compare two elements. Counting sort treats each key as an index — tally the keys, turn tallies into positions with a prefix sum, write every element straight to its slot in O(n + k). Radix sort closes the unit by running that stable trick once per digit, sorting numbers too wide for one counting pass — and quietly demonstrating why stability was worth all the fuss.

By the last one, the table above stops being a list to memorize and starts being something you can reconstruct, because you have watched where every number in it comes from.

All Sorting Visualizations

Pick any sort below to open its interactive player — change the input, step through it, and read the code highlight move in lockstep with the animation.