Free sorting algorithm visualizer. Watch bubble, insertion, selection, merge and quicksort run step by step on random, nearly sorted or reversed data, with live comparison and write counts.
Knobs patch the running animation live. A few (marked in the code as baked-in values like particle counts) restart the preview.
Five sorts as generators, stepped a fixed number of operations per frame.
Your code runs in a sandboxed frame with no access to this page, and it is never sent to a server. three.js demos load the library from jsDelivr; the rest need nothing but the browser.
Bar height is the value being sorted and red marks the two positions involved in the current comparison or swap. The counters below the chart track comparisons and writes separately, which matters because the two costs are not interchangeable: selection sort performs the fewest writes of anything here while doing the same number of comparisons regardless of input, and bubble sort does the most writes of all.
Set the array size to 200 and run each algorithm on the same data. The gap between the quadratic sorts and the n log n sorts stops being an abstraction once you watch the counters climb side by side.
The Starting data control is the most instructive part of this tool, because algorithm performance is a property of the input as much as the algorithm.
The obvious way to animate a sort is to put a short sleep inside the inner loop. It works once, then becomes unusable: the speed cannot be changed mid-run, restarting leaves half-finished recursive calls running, and the sort is no longer the algorithm you were trying to show.
Every sort here is written as a generator that yields once per comparison and once per write. The animation loop pulls a fixed number of steps per frame, so speed is a property of the renderer rather than the algorithm, and restarting is simply discarding the generator. Recursion still works normally through yield delegation, which is why quicksort and merge sort suspend and resume as cleanly as the flat loops do.
Bubble sort, insertion sort, selection sort, merge sort and quicksort. Each runs on the same bar chart so you can compare them directly on identical data.
Because its cost is the number of inversions in the input, not n squared. On nearly ordered data there is almost nothing to move. That is exactly why real library sorts fall back to insertion sort for small or nearly sorted runs.
Selection sort, at one swap per pass. It still performs the same number of comparisons no matter what the input looks like, so its shape on screen never changes. Bubble sort sits at the other extreme and writes the most.
Bar height is the value. Red marks the two positions being compared or swapped in the current step.
To show why the last element is a poor pivot choice. On reversed input that pivot produces maximally unbalanced partitions and quicksort degrades towards n squared.
Every sort is a JavaScript generator that yields once per comparison and once per write. The render loop pulls a fixed number of steps per frame, so speed is adjustable mid-run and restarting means discarding the generator. Recursive sorts suspend and resume through yield delegation.
Yes. The HTML, CSS and JavaScript panes are editable and the code runs exactly as shown, so you can change an algorithm and watch the result immediately.