Sorting Algorithm Visualizer

Watch bubble, insertion, selection, quick, merge and heap sort run on the same bars, one comparison or swap at a time. Every run is counted: comparisons, swaps or writes, passes, and a pass-by-pass trace of your own array.

Visualizer Code Lab Updated Oct 5, 2026
How to Use
  1. Pick an algorithm in the bar: Bubble, Insertion, Selection, Quick, Merge or Heap. The readouts show its comparisons, swaps and passes on the current array straight away.
  2. Choose the starting order with a preset (Random, Nearly sorted, Reversed, Few unique, Already sorted) and the Size slider (5–160 bars), or type your own numbers in the Array box and press Enter.
  3. Press Play (or Space) to animate. Step and Back (→ and ←) move one comparison or swap at a time, and the timeline jumps anywhere in the run.
  4. Read Show Work below the tool: every pass, partition or merge on your array, with its own comparison and swap counts and the array after it.
  5. Compare best and worst cases: keep the array and switch algorithm, or keep the algorithm and switch preset. Bubble sort on 16 already-sorted items makes 15 comparisons; quicksort makes 120.
  6. Share the exact run: the address keeps ?algo=, ?preset= and ?size=, or ?array=5,1,4,2,8 for your own numbers.
Input
2–160 numbers, commas or spaces
16
40 steps/s
Presets
Keys: Space play / pause · → step · ← back · Home start · End finish
Bars
Loading the run…
Step 0
Comparisons
120
Swaps
58
Passes
15
Best / avg / worst
n · n² · n²
Bubble sort on 16 items · stable · in place · 116 array writes · 58 inversions in the input

Worked Example

Bubble sort on [5, 1, 4, 2, 8]. Each pass compares neighbours from the left and swaps any pair in the wrong order, so the largest remaining value ends up at the right; a pass with no swaps means the array is sorted.

Pass 1 (a[0..4]): 5 > 1, swap → [1, 5, 4, 2, 8]; 5 > 4, swap → [1, 4, 5, 2, 8]; 5 > 2, swap → [1, 4, 2, 5, 8]; 5 < 8, no swap. 4 comparisons, 3 swaps; 8 is final.
Pass 2 (a[0..3]): 1 < 4; 4 > 2, swap → [1, 2, 4, 5, 8]; 4 < 5. 3 comparisons, 1 swap; 5 is final.
Pass 3 (a[0..2]): 1 < 2; 2 < 4. 2 comparisons, 0 swaps, so stop.
Total: 9 comparisons, 4 swaps, 3 passes. The input has 4 inversions, (5, 1), (5, 4), (5, 2) and (4, 2), and bubble sort always makes exactly one swap per inversion. Run this example.

Insertion sort on the same array. Each pass takes the next item and swaps it left while it is smaller than its neighbour. Insert 1: 1 comparison, 1 swap → [1, 5, 4, 2, 8]. Insert 4: 2 comparisons, 1 swap → [1, 4, 5, 2, 8]. Insert 2: 3 comparisons, 2 swaps → [1, 2, 4, 5, 8]. Insert 8: 1 comparison, 0 swaps. Total: 7 comparisons, 4 swaps: the same 4 swaps (one per inversion) but 2 fewer comparisons, because each pass stops as soon as the item is in place. Run it.

The common mistake: “quicksort is always O(n log n)”. This quicksort uses the last item of each range as the pivot. On [1, 2, 3, 4, 5, 6, 7, 8], already sorted, the pivot is always the largest item, so each partition compares every other item with it and splits off only the pivot: 7 + 6 + 5 + 4 + 3 + 2 + 1 = 28 comparisons in 7 partitions, 7 levels deep, with 0 swaps. That is n(n − 1)/2, exactly bubble sort’s worst case. The same eight values in the order [3, 2, 7, 8, 5, 4, 1, 6] take 16 comparisons and 6 swaps in 5 partitions: the first pivot, 6, lands at a[5] with 5 items on its left and 2 on its right. Quicksort is O(n log n) on average and O(n²) in the worst case, which is why algs4 shuffles the array before sorting. Run the worst case.

Show Work

The trace of the current array appears here: each pass, partition or merge with its own comparison and swap counts.

How It Works

Bubble sort
best n · avg n² · worst n²
Stable, in place, O(1) extra memory. Compare neighbours and swap if out of order; the largest value reaches the end of each pass. Stops after a pass with no swaps, so sorted input takes n − 1 comparisons. Worst case about ½n² comparisons and swaps (algs4 Bubble.java).
Insertion sort
best n · avg n² · worst n²
Stable, in place, O(1). Swap each new item left until it is in place. About ¼n² comparisons and swaps on random input, ½n² on reversed input, n − 1 comparisons and 0 swaps on sorted input. Swaps = inversions exactly (algs4 §2.1).
Selection sort
best n² · avg n² · worst n²
Not stable, in place, O(1). Find the minimum of the unsorted part and swap it to the front. Always n(n − 1)/2 comparisons, whatever the order, but at most n − 1 swaps (algs4 §2.1: about ½n² compares and n exchanges).
Quicksort
best n log n · avg n log n · worst n²
Not stable, in place; the recursion uses stack space. Partition around a pivot (here the last item), then sort each side. About 2n ln n comparisons on average; about ½n² in the worst case, such as sorted input with this pivot rule (algs4 §2.3).
Merge sort
best n log n · avg n log n · worst n log n
Stable, not in place: O(n) extra memory for the runs. Sort each half, then merge, taking the left item on a tie. Top-down merge sort uses between ½ n lg n and n lg n comparisons (algs4 §2.2).
Heap sort
best n log n · avg n log n · worst n log n
Not stable, in place, O(1). Build a max-heap by sifting down from the middle, then repeatedly swap the root (the largest) to the end and sift down again. Sink-based heap construction is linear; the whole sort uses fewer than 2n lg n comparisons (algs4 §2.4).
Inversions
inv = #{ i < j : a[i] > a[j] }
0 for sorted input, n(n − 1)/2 for reversed input. Bubble and insertion sort make exactly inv swaps, because a neighbour swap removes exactly one inversion. Show Work prints inv for your array.
Lower bound
C ≥ lg(n!) ~ n lg n
No sort that works only by comparing items can guarantee fewer comparisons than this; for 16 items lg(16!) = 44.3. Merge sort and heap sort meet it up to a constant factor (algs4 §2.2).

Counting rules. A comparison is one test of two keys. A swap exchanges two different positions; an item is never swapped with itself. Writes are 2 per swap plus 1 for every single item written, which is how merge sort moves items. These are the counts in the readouts and in Show Work, and the Worked Example uses the same rules.

Sources: R. Sedgewick and K. Wayne, Algorithms, 4th ed., sections 2.1 Elementary Sorts, 2.2 Mergesort, 2.3 Quicksort, 2.4 Priority Queues and 2.5 Sorting Applications (stability and in-place table), plus Bubble.java; CPython’s Objects/listsort.txt for Timsort; MDN’s Array.prototype.sort() for JavaScript.

Where These Algorithms Come From

Bubble sort is older than its name. Owen Astrachan’s “Bubble Sort: An Archaeological Algorithmic Analysis” (SIGCSE 2003) traced it through the 1950s literature, where it was called sorting by exchange; a 1956 paper by E. Friend in the Journal of the ACM analysed it under that name. Kenneth Iverson’s A Programming Language (1962) appears to be the first use of “bubble sort” in print. It entered the ACM’s published algorithms in 1963 as Algorithm 175, “Shuttle Sort”, and a correction later that year gave the early-stopping version and said it had been studied on the ORDVAC computer at Aberdeen Proving Ground in 1955. Donald Knuth’s verdict, quoted in the paper: it “seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems.”

Quicksort was invented in 1960 by C. A. R. (Tony) Hoare. It was published as Algorithm 64 in Communications of the ACM in July 1961, and Hoare’s paper “Quicksort” appeared in The Computer Journal, volume 5, pages 10–16, in 1962. Heapsort was published by J. W. J. Williams as Algorithm 232 in Communications of the ACM in June 1964.

The sorts in everyday languages are refinements of these ideas. Python’s documentation names its algorithm as Timsort, which CPython’s design notes (Objects/listsort.txt) describe as “an adaptive, stable, natural mergesort”; the What’s New page for Python 2.3, released on 29 July 2003, records that list.sort() had been “extensively rewritten by Tim Peters”. JavaScript left the algorithm to each engine, but since ECMAScript 2019 the specification requires Array.prototype.sort to be stable.

About This Tool

This visualizer runs a real implementation of each of the six classic comparison sorts on a copy of your array, records every comparison, swap and write, and then replays that record as bars, so the animation and the counts come from the same run. Bubble, insertion and selection sort are traced pass by pass, quicksort partition by partition, merge sort merge by merge and heap sort sift-down by sift-down, each with its own counts and the array after it.

What it adds to a plain animation: exact counts with stated rules that match the Worked Example, a check of each run against the textbook formulas (swaps = inversions, n(n − 1)/2, n lg n), repeated values labelled a, b, c so stability can be seen rather than taken on trust, and a URL for every run. Everything runs in your browser; nothing you type is uploaded.

It is for students tracing sorts for a course or an interview, teachers who want to show best and worst cases side by side, and programmers checking their own sorting code against known counts.

Related tools: Execution Visualizer, Code Inspector, and Graph Theory Lab.

Frequently Asked Questions

Why does quicksort make 120 comparisons on an already sorted array?

This quicksort uses the last item of each range as the pivot. On sorted input that is always the largest item, so every partition puts nothing on its right and splits off just one item: 15 + 14 + … + 1 = 120 comparisons for 16 items, which is n(n − 1)/2, the same as bubble sort’s worst case, with recursion 15 levels deep. On the Random preset the same 16 values take 50 comparisons and 6 levels. Sedgewick and Wayne’s algs4 shuffles the array first for exactly this reason: quicksort is about 2n ln n on average but n²/2 in the worst case.

What exactly counts as a comparison and a swap?

A comparison is one test of two keys, such as a[j] > a[j+1] in bubble sort or a[q] < pivot in quicksort. A swap exchanges two different positions; an item is never counted as swapped with itself (algs4’s selection sort counts n exchanges because it does exchange an item with itself, so this tool reports at most n − 1). Merge sort does not swap: it copies items into two runs and writes them back, so its second readout is writes. Array writes for every algorithm are in the line under the readouts.

Which sorting algorithms are stable?

Bubble, insertion and merge sort are stable: equal values keep their original order. Selection sort, quicksort and heap sort are not (algs4 §2.5). Try the Few unique preset: 16 values from 1 to 4, with repeats labelled a, b, c in input order. Bubble, insertion and merge sort keep every 1a before 1b; selection, quick and heap sort move some equal values past each other on that array, and Show Work says so. JavaScript’s Array.prototype.sort has had to be stable since ECMAScript 2019, and Python’s sort is stable too.

Why do bubble sort and insertion sort make the same number of swaps?

Both only ever swap neighbours that are out of order, and each such swap removes exactly one inversion (a pair i < j with a[i] > a[j]). So both make exactly as many swaps as the input has inversions: 58 on the default random 16-item array, 120 on 16 reversed items. Insertion sort needs fewer comparisons (71 against 120 on the default array) because each pass stops as soon as the new item is in place; algs4 proves its comparisons lie between the inversions and the inversions plus n − 1.

Which sort do JavaScript and Python actually use?

Python’s list.sort() is Timsort, described in CPython’s listsort.txt as an adaptive, stable, natural merge sort that needs as few as n − 1 comparisons on data that is already ordered. JavaScript engines choose their own algorithm, but the language has required a stable sort since ECMAScript 2019. One trap in JavaScript: with no compare function, sort() compares numbers as strings, so [10, 9, 1].sort() gives [1, 10, 9]. Pass (a, b) => a − b to sort numbers.

How do I use the Sorting Algorithm Visualizer?

Just type your numbers. The answer shows up right away — there is no button to press. Change anything and it updates by itself.

Is it free? Does it work without internet?

Yes to both. It is free with no sign-up, and once the page has loaded it keeps working even with no internet.

Where does my data go?

Nowhere — every calculation runs on your own device. Nothing you enter is uploaded, logged, or stored.

Common Use Cases

Check a hand trace

Trace bubble sort on [5, 1, 4, 2, 8] on paper, then load it here: 3 passes, 9 comparisons and 4 swaps, with the array after each pass in Show Work.

See what Big-O means

On 160 random items bubble sort makes 12,720 comparisons and merge sort 972, about 13 times fewer. Double the size and watch which count roughly quadruples.

Interview prep on worst cases

Quicksort with a last-item pivot on 16 sorted items: 120 comparisons, 15 partitions, 15 levels deep. On the Random preset: 50 comparisons, 11 partitions, 6 levels.

Teach stability

Load Few unique (values 1–4 with repeats labelled a, b, c) and run selection sort: Show Work reports that equal values changed order. Merge sort on the same array keeps them.

Test your own sort

Paste the array your program sorts and compare its counters with the readouts. Insertion sort on [5, 1, 4, 2, 8] should give 7 comparisons and 4 swaps.

Last updated: