Interactive · Sorting Algorithms
Sorting Algorithm Visualizer
Watch bubble, insertion, selection, merge, quick and heap sort rearrange a bar chart step by step. Amber bars are being compared, red were just swapped, green are locked in place — and the counters show exactly how many comparisons and writes each one takes.
Pick an algorithm, set the array size and speed, then press Sort. Shuffle for a fresh random array.
Six sorts, two speed classes
- Bubble, insertion & selection are the O(n²) sorts — simple, in-place, and fine for tiny arrays, but the comparison counter explodes as the array grows. Insertion sort is the pick of the three: it's fast on nearly-sorted data and real libraries use it for small chunks.
- Merge, quick & heap are the O(n log n) sorts. Merge sort is stable and predictable but uses extra memory; quicksort is usually fastest in practice but has an O(n²) worst case; heapsort guarantees O(n log n) in constant space.
Run the same array size through an O(n²) sort and then quicksort with the comparison counter visible — the gap between them is the whole reason big-O matters. The companion write-up counts the exact operations each of the six sorts makes on 50 elements, and shows why quicksort collapses to O(n²) on an already-sorted array.
More algorithm visualizers
Sorting is one family of algorithms; search is another. Watch BFS, Dijkstra and A* find shortest paths on a grid in the Pathfinding Visualizer, or see the game-playing algorithms — minimax, alpha-beta and expectimax — in the Game AI Visualizer. Everything runs client-side; nothing you do here leaves your browser.
Frequently asked questions
What is the fastest sorting algorithm?
For general data, the O(n log n) sorts — merge sort, quicksort and heapsort — are far faster than the O(n²) sorts (bubble, insertion, selection) as the array grows. Quicksort is usually the quickest in practice because its inner loop is tight and cache-friendly, though its worst case is O(n²); merge sort guarantees O(n log n) but needs O(n) extra memory; heapsort guarantees O(n log n) in O(1) space. On tiny or nearly-sorted arrays, insertion sort can win outright, which is why real libraries switch to it for small partitions.
What is the difference between bubble sort and quicksort?
Bubble sort repeatedly compares adjacent elements and swaps them, making O(n²) comparisons — simple but slow. Quicksort picks a pivot, partitions the array so smaller elements go left and larger go right, then recurses, averaging O(n log n). Run both on the same array in the visualizer with the comparison counter on: bubble sort makes thousands of comparisons where quicksort makes hundreds.
What do the colours mean?
Each bar's height is a value. Amber bars are the two elements currently being compared; red bars were just swapped or overwritten; green bars are in their final sorted position. Blue bars are unsorted. Watching the amber and red move shows exactly how each algorithm makes its decisions.
Are these the sorting algorithms used in real software?
Yes — merge sort, quicksort and heapsort are the basis of the sort functions in most standard libraries, usually as a hybrid (for example Timsort, a merge/insertion blend, or introsort, a quick/heap blend). The O(n²) sorts are mainly taught to build intuition, though insertion sort is genuinely used for very small or nearly-sorted arrays. This tool runs the textbook versions so you can see how each behaves.