From O(N²) classics to O(N log N) divide-and-conquer sorting algorithms.
6 algorithms2 races6 problems
6 algorithms. Each one builds on the last.
Same input, side by side. Watch who wins.
Why the n log n sorts finish first, and when a slow sort is still the right one.
Watch the raceHow fast each Big-O class grows, timed on one input and one computer.
Watch the raceSorting puts values in order, and much other work gets cheaper once data is sorted: binary search, merging lists, spotting duplicates, finding a median. Interviews use sorts to test reasoning about time, memory and stability.
All six are comparison sorts: they learn the order only by comparing two values at a time, and each is a different plan for which pairs to compare. That is why none can beat about n log n comparisons in the worst case, and why the simple ones, which compare neighbours or rescan everything left, land at O(n²). The fast three cut the work by splitting the array or by keeping a heap.
A quick rule: small or nearly sorted input, insertion sort. Stability or a firm O(n log n) bound, merge sort. That bound with no spare memory, heap sort. Most other in-memory work, quick sort with a sensible pivot. Its average hides an O(n²) worst case, which is why libraries pick pivots at random or fall back to heap sort when recursion runs deep.