Sorting algorithm visualiser
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.