Why the n log n sorts finish first, and when a slow sort is still the right one.
About this comparison
Eight sorts start on the same 24 shuffled bars. Each operation, a comparison, swap, move or digit read, takes the same time in every panel, so the finishing order is the order of the counts.
The simple sorts work one pass at a time. Bubble sort swaps neighbours pass after pass. Selection sort finds the smallest of the rest and swaps it to the front. Insertion sort swaps each bar left until the one before it is smaller. Shell sort runs insertion sort over shrinking gaps, so far-off bars jump early.
The fast ones divide the work. Merge sort sorts each half and merges the runs. Quick sort splits each range around its last bar. Heap sort keeps moving a max-heap’s top to the end. Radix sort never compares: it reads each bar’s digit in base 4 and places the bars by it, digit by digit.
Bubble, selection and insertion sort make O(n²) comparisons on shuffled input. Merge sort, heap sort and, on average, quick sort make O(n log n), and shell sort lands between the two groups. Radix sort takes one pass per digit. Bubble, insertion and merge sort are stable; selection, quick and heap sort are not.
The counts on these bars are merge sort 124, radix sort 144, quick sort 164, shell sort 185, heap sort 228, selection sort 298, insertion sort 321 and bubble sort 416, and that is the finishing order. Bubble sort and insertion sort both make 150 swaps, one for each pair of bars out of order; bubble spends more comparisons finding them. Selection sort makes just 22 swaps but 276 comparisons.
On more bars the gap only widens: n² outgrows n log n.
Day to day, use the language’s built-in sort: a tuned hybrid of merge sort or quick sort with insertion sort for short runs. Use insertion sort alone on small or nearly sorted lists, where it runs close to O(n). Use heap sort when the worst case must stay O(n log n) with no extra memory, and radix sort for many integer keys with few digits.
Bubble sort is for teaching. Selection sort’s few swaps help only when a write costs far more than a read.