Visualise and compare 6 sorting algorithms side by side. Watch bubble, insertion, selection, merge, quick, and heap sort step through arrays with animated bar heights. See O(n²) vs O(n log n) in action.
Bubble sort O(n²): repeatedly swap adjacent elements. Insertion sort O(n²): insert each element into its correct position — efficient for nearly-sorted data. Selection sort O(n²): find minimum and place it. Merge sort O(n log n): divide and merge — stable, predictable. Quicksort O(n log n) avg: pivot partition — fast in practice. Heap sort O(n log n): use heap structure — in-place.
Insertion sort excels on nearly-sorted data: O(n) best case. Bubble sort also O(n) best with optimisation. Quicksort degrades to O(n²) on already-sorted arrays with naive pivot — random pivot or median-of-3 fixes this. Merge sort and Heap sort always O(n log n) regardless of input.
Real-world sort libraries (like Timsort used in Python/Java) combine insertion sort for small arrays with merge sort for large ones. Cache locality matters: quicksort's sequential memory access often beats merge sort despite same big-O. Comparison count is not the only metric — swap count and cache performance both matter.
This visualiser runs six classic sorting algorithms side by side on the same starting array, so you can watch Bubble, Insertion, Selection, Merge, Quick, and Heap sort race in real time. Each canvas redraws bar heights every frame from a JavaScript generator function, highlighting the two indices currently being compared (amber) and swapped (red) while sorted regions turn green. It's a hands-on way to see why O(n log n) algorithms pull ahead of O(n²) ones as array size grows.
Six canvases run independently using JS generator functions (yield) that pause after every comparison, letting all six algorithms animate in lockstep. Bar height encodes value, colour encodes role: amber = compared, red = swapped, green = finished.
Adjust Array Size (10-80) and Speed (1-30 steps per frame) with the sliders, pick a starting layout with the Random / Nearly Sorted / Reversed / All Same preset buttons, then press Sort All to race them and New Array to reshuffle.
On a nearly-sorted array, Insertion Sort can finish in close to O(n) time — faster in practice than the "better" O(n log n) algorithms for small, almost-ordered inputs, which is why real libraries like Timsort fall back to it.
They belong to the O(n log n) complexity class, which grows much more slowly than the O(n²) class (Bubble, Insertion, Selection) as the array size increases. With the default 40-element array the gap is visible; push Array Size to 80 and it becomes dramatic.
This implementation always picks the last element as the pivot. On an already-sorted or reversed array that choice is worst-case for Quick Sort, degrading it toward O(n²) instead of its average O(n log n) — a well-known weakness fixed in practice with random or median-of-three pivots.
The "Bubble comparisons" and "Merge comparisons" counters tally every element-to-element comparison (the c variable in each generator) made so far, letting you compare algorithmic cost directly rather than just visual speed.
It starts from a fully sorted array of 1..n and then performs roughly n/10 random swaps, simulating data that is mostly in order — the scenario where Insertion and Bubble sort perform closest to their best-case O(n).
Yes — because it always splits the array in half and merges regardless of the input's order, its runtime doesn't depend on how sorted the data already is, unlike Quick Sort or Insertion Sort whose speed varies with input order.