Sorting visualizer
Watch classic sorting algorithms work one comparison at a time, with live counts of comparisons, swaps, writes and the inversions still left to fix.
Whole numbers from 0 to 999, separated by spaces or commas; between 2 and 110 of them.
- Time
- Best case
- Extra space
- Stable
What is an inversion?
An inversion is a pair of positions i < j whose values are out of order: a[i] > a[j]. A sorted array has 0 inversions; a reversed array has the maximum, n(n−1)/2.
Every swap of two adjacent elements fixes exactly one inversion. That is why bubble sort's swap count always equals the initial inversion count, and why nearly sorted inputs sort so much faster with insertion sort.
Sorting is finished exactly when the “Inversions left” counter reaches 0.
Keys: Space play or pause, ← → step.