Sorting algorithm visualiser

Watch four sorting algorithms work on the same list at the same time, with a live count of comparisons and swaps, so the difference between them stops being an abstract claim.

Everybody can recite that bubble sort is slow and merge sort is fast. Far fewer students can say what the algorithms are actually doing differently, which is the part worth marks. Watch all four run on the same list and the difference stops being an abstract claim.

Sorting algorithm visualiser

Run bubble, insertion, merge and quick sort side by side and count the comparisons. This tool needs JavaScript, so turn it on to use it. Everything you need to answer the exam question is explained in the text above.

What to look for

  • Bubble sort compares the same neighbours over and over, and the largest value walks to the end on every pass
  • Insertion sort grows a sorted section on the left and pushes each new value back into place
  • Merge sort barely compares at all until it starts merging, and the number of comparisons hardly changes when you shuffle the list
  • Quick sort is usually the fastest here, but its comparison count swings the most between shuffles, which is exactly why its worst case is so much worse than its average case
Best, average and worst

Bubble and insertion sort are n squared in the worst case. Merge sort is n log n in every case. Quick sort is n log n on average but n squared in the worst case. Learn those six facts and most sorting questions answer themselves.