Sorting Algorithm Visualizer

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.

Sorting Visualizer

Live controls

90
6
205

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.

Pick an animation

All 40 animations →

Canvas 2D
three.js
WebGL
CSS / DOM
Advertisement

How to read the visualization

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.

Why the starting data changes everything

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.

  • Nearly sorted makes insertion sort finish almost immediately, because its real cost is the number of inversions rather than the array length squared. This is why production sorts such as Timsort detect existing runs and fall back to insertion sort for them.
  • Reversed is the worst case for a quicksort that takes the last element as its pivot: every partition is maximally unbalanced and the recursion depth grows to the length of the array.
  • Random is the case most textbooks quote, and the only one where the quoted average complexities apply directly.

Generators instead of sleeps

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.

Frequently Asked Questions

Which sorting algorithms does it show?+

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.

Why does insertion sort look slow on random data but fast on nearly sorted 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.

Which algorithm does the fewest writes?+

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.

What do the red bars mean?+

Bar height is the value. Red marks the two positions being compared or swapped in the current step.

Why run quicksort on reversed data?+

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.

How is the animation implemented?+

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.

Can I see the code?+

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.

This tool is provided for informational and educational purposes only. All processing happens in your browser — no data is sent to or stored on our servers. While we strive for accuracy, we make no warranties about the completeness or reliability of results.