Sorting algorithm visualizer: race seven sorts side by side
Race up to seven sorting algorithms against each other on the same input, from 8 to 100 values, then slow any one of them down and step through it line by line. Bubble, insertion, selection, merge, quick, heap and counting sort, all running in your browser.
On this page
Sorting raceEvery algorithm sorts the same input, one step (a comparison, swap or write) per tick.
Press play to start the race.
How to use the race
- Choose the input. Random data is the usual benchmark, but the interesting results come from the others: nearly sorted data (a few neighbours swapped), reversed data, data that is already sorted, and data with only four distinct values. Drag the size slider to set how many values there are; each change deals a new input.
- Pick the runners with the checkboxes. Four are selected to start with; tick all seven for the full field.
- Press play. On every tick, each algorithm performs the same number of basic steps: one comparison, one swap or one write each. The lane that finishes first did the least work on this input, not merely the work fastest. Use “Steps per tick” to speed up large inputs, and the step button to advance a single step.
Bars turn green once a value is in its final position. Colours mean the same as in the step-through visualiser below: amber for a comparison, red for a swap, purple for a write into the array.
Step through one algorithm
The race shows how much work each algorithm does. To see why, run one algorithm on its own. This visualiser narrates every step, highlights the line of pseudocode being executed, and shows the auxiliary array for merge and counting sort. You can type your own values (up to 40) and scrub backwards and forwards.
Sorting visualiserPick an algorithm, edit the values, then press play or step through.
- Comparing
- Swapping
- Writing
- Pivot / heap root
- Key / current minimum
- In final place
Pseudocode
Counters
This algorithm
Compare all seven algorithms on this input
| Algorithm | Comparisons | Swaps | Writes | Worst case | Stable? |
|---|
A swap exchanges two entries; a write stores one value (merge, insertion and counting sort move values by writes). Counting sort never compares two elements, which is how it beats the n log n lower bound for comparison sorts.
Benchmark: steps on 50 values
This is what the race reports for one fixed set of 50 values in each arrangement: comparisons first, then moves (swaps plus writes). The lowest total in each column is in bold. Your runs will differ in detail because the slider deals new random values, but the pattern is stable.
| Algorithm | Random | Nearly sorted | Reversed | Few unique |
|---|---|---|---|---|
| Bubble sort | 1215 + 467 | 144 + 3 | 1225 + 1211 | 1197 + 458 |
| Insertion sort | 515 + 516 | 52 + 52 | 1224 + 1260 | 506 + 507 |
| Selection sort | 1225 + 48 | 1225 + 3 | 1225 + 26 | 1225 + 36 |
| Merge sort | 223 + 286 | 154 + 286 | 145 + 286 | 216 + 286 |
| Quicksort | 269 + 121 | 879 + 16 | 1034 + 194 | 437 + 78 |
| Heapsort | 409 + 243 | 436 + 264 | 377 + 204 | 389 + 204 |
| Counting sort | 0 + 50 | 0 + 50 | 0 + 50 | 0 + 50 |
Things worth noticing:
- Insertion sort wins on nearly sorted data, despite its worst case. Each value only slides a short distance, so the work is close to linear. This is why library sorts such as Timsort switch to insertion sort for short runs.
- Reversed input is the worst case for bubble and insertion sort: every pair is out of order, so they make about comparisons and as many moves.
- Selection sort does not care about the arrangement. It always makes exactly = 1225 comparisons, but never more than swaps, which matters when writing is expensive.
- Merge and heap sort are steady: their counts barely move between arrangements, as their bound in every case predicts.
- Counting sort makes no comparisons at all. It counts how often each value occurs and writes them back in order, so it does about steps for values below . It only works for small integer keys.
Complexity at a glance
| Algorithm | Best | Average | Worst | Extra space | Stable |
|---|---|---|---|---|---|
| Bubble sort | Θ(n) | Θ(n²) | Θ(n²) | Θ(1) | Yes |
| Insertion sort | Θ(n) | Θ(n²) | Θ(n²) | Θ(1) | Yes |
| Selection sort | Θ(n²) | Θ(n²) | Θ(n²) | Θ(1) | No |
| Merge sort | Θ(n log n) | Θ(n log n) | Θ(n log n) | Θ(n) | Yes |
| Quicksort | Θ(n log n) | Θ(n log n) | Θ(n²) | Θ(log n) stack on average | No |
| Heapsort | Θ(n log n) | Θ(n log n) | Θ(n log n) | Θ(1) | No |
| Counting sort | Θ(n + m) | Θ(n + m) | Θ(n + m) | Θ(m) for the counts | Yes |
A sort is stable if equal values keep their original order. That matters when you sort records by one field after another: sort by first name, then stably by surname, and people with the same surname stay in first-name order. Use the “Few unique values” input in the step-through visualiser to watch an unstable sort such as selection sort reorder equal values.
Where to go next
The sorting algorithms explainer covers how each algorithm works, why no comparison sort can beat , and what real libraries use. Big-O notation explains what the complexity classes in the table mean, and the recursion tree visualiser draws the call tree of merge sort’s recurrence.