put a row in order
Eleven sorting methods put a row in order. Race two to four of them on the same row and watch which finishes first.
or make one
race these, up to four
Choose two to four racers.
one tick is
Without scripts the race cannot run, so each lane shows where its racer ends.
Tick 26: no lane has made more than 26 comparisons. Insertion sort, merge sort and quick sort have finished.
No tick the race offers is counted by every one of these racers, so one clock cannot time them. Choose others.
bubble sort
cocktail shaker sort
insertion sort
done in 26 comparisonsselection sort
shell sort
quick sort
done in 19 comparisonsrandomised quick sort
merge sort
done in 22 comparisonsheap sort
counting sort
radix sort
what the marks mean
- a number being compared
- a number moving
- a number placed for good
- a number its sort holds aside
- a number not yet placed for good
- how much of its race a lane has run
which suits this row
On these 9 numbers, quick sort made the fewest comparisons: 19, against 22 for merge sort and 26 for insertion sort.
each racer's own page
- bubble sort
- cocktail shaker sort
- insertion sort
- selection sort
- shell sort
- quick sort
- randomised quick sort
- merge sort
- heap sort
the starting racers as the row grows
This row of 9 suits quick sort. On shuffled rows of 1,000 and of a million, merge sort makes the fewest comparisons, on average.
Their comparisons on the starting row, once, then on shuffled rows, on average. On the shuffled rows, a plain number is measured, the mean of 20 rows, and a number in italics comes from its racer's own formula: follow its link. The fewest on each line is in bold.
| insertion sort | quick sort | merge sort | |
|---|---|---|---|
| the starting row8, 3, 7, 2, 5, 9, 4, 1, 6 | 26 | 19 | 22 |
| 1,000 numbers | 249,769 | 10,831 | 8,720 |
| 1,000,000 numbers | 250,000,749,986 | 24,785,482 | 18,715,960 |
the starting racers on 9 numbers of each shape
Shape can change the winner. On the rows below, insertion sort wins in order, nearly in order and with many repeats; merge sort wins back to front.
Their comparisons on the row the race makes in each shape.
| insertion sort | quick sort | merge sort | |
|---|---|---|---|
| shuffled8, 3, 7, 2, 5, 9, 4, 1, 6 | 26 | 19 | 22 |
| in order1, 2, 3, 4, 5, 6, 7, 8, 9 | 8 | 36 | 20 |
| back to front9, 8, 7, 6, 5, 4, 3, 2, 1 | 36 | 36 | 13 |
| nearly in order1, 2, 4, 3, 5, 6, 8, 7, 9 | 10 | 28 | 20 |
| many repeats2, 1, 2, 3, 3, 1, 2, 3, 2 | 17 | 20 | 21 |