Skip to content

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

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.

insertion sort

done in 26 comparisons
123456789

quick sort

done in 19 comparisons
123456789

merge sort

done in 22 comparisons
123456789

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

The same racers on bigger shuffled rows: average comparisons up, how many numbers across, each labelled grid line ten times the last. A solid line is measured; a dashed one carries on the formula its own page derives.
10210410610810101012101410110210310410510610710110210310410510610710810910101011101210131014101102103104105106107
  • 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 sortquick sortmerge sort
the starting row8, 3, 7, 2, 5, 9, 4, 1, 6261922
1,000 numbers249,76910,8318,720
1,000,000 numbers250,000,749,98624,785,48218,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 sortquick sortmerge sort
shuffled8, 3, 7, 2, 5, 9, 4, 1, 6261922
in order1, 2, 3, 4, 5, 6, 7, 8, 983620
back to front9, 8, 7, 6, 5, 4, 3, 2, 1363613
nearly in order1, 2, 4, 3, 5, 6, 8, 7, 9102820
many repeats2, 1, 2, 3, 3, 1, 2, 3, 2172021