Sorting Algorithm Visualizer
Type your own numbers or generate some, pick an algorithm and watch it sort them one step at a time, with the pseudocode line and a plain-English explanation of every comparison, swap and write. Step backwards to replay anything you missed, or race two algorithms on the same data. New to sorting? Follow the sorting algorithms roadmap.
Data
What's happening
About this algorithm
How to use the sorting visualizer
Each bar is one number; taller bars are larger values. Press Play to animate, or use ◀ and ▶| (or the arrow keys) to go one step at a time while reading the explanation and the highlighted pseudocode line. Drag the bar under the controls to jump to any point. The counters show how many comparisons, swaps and writes the algorithm has done so far, which is the best way to feel the difference between O(n²) and O(n log n). In Race mode both algorithms sort the same numbers and do one operation each per tick, so the one that needs fewer operations finishes first.
Try these: run insertion sort on Nearly sorted data (almost linear), quicksort on Reversed data (its worst case, because this version picks the last item as the pivot), and counting sort on Few unique (zero comparisons at all).
Sorting algorithms compared
| Algorithm | Best | Average | Worst | Extra space | Stable |
|---|
Stable means equal values keep their original order, which matters when you sort records by one field after another. n is the number of items, k the range of values and d the number of digits.
Which sort should I use?
In real code, call your language's built-in sort: Python and Java (for objects) use Timsort, a stable merge/insertion hybrid that is very fast on partly sorted data, and C++ and Rust use introsort or pattern-defeating quicksort. Learn the classic algorithms because they teach the ideas behind them: divide and conquer (merge sort, quicksort), heaps (heap sort), and beating O(n log n) when values are small integers (counting and radix sort). They also come up constantly in coding interviews.
Everything runs in your browser; your numbers are never sent anywhere.