Skip to content

Sorting Algorithm Visualizer

Pick an algorithm, enter some numbers or generate an input from a seed, and step through the sort one comparison, swap or write at a time. The counters show exactly how much work each step costs, and a table at the bottom runs all six algorithms on your input so you can compare them. Free, no sign-up, and everything runs in your browser.

Algorithm

Between 2 and 40 numbers, separated by commas, spaces or new lines. Negatives, decimals and repeated values all work.

Or generate an input

The same seed and pattern always give the same input, so a run can be repeated or shared.

Walkthrough

  • Being compared
  • Just swapped
  • Just written
  • Pivot, minimum or heap root
  • In its final position

Step 1 of 95Start: 12 values, not yet sorted.

0

Comparisons

0

Swaps

0

Array writes

About Bubble sort

Compares neighbouring values and swaps any pair that is out of order, so the largest remaining value bubbles to the end on each pass. It stops early if a pass makes no swaps.

Time and space cost
PropertyValue
Best case timeO(n)
Average case timeO(n²)
Worst case timeO(n²)
Extra spaceO(1)
Stable (equal values keep their order)Yes

All six on your input

Operation counts when each algorithm sorts exactly the numbers above. Try the patterns to see the best and worst cases move.

Comparisons, swaps and writes by algorithm
AlgorithmComparisonsSwapsArray writes
Bubble sort563060
Insertion sort39039
Selection sort66816
Merge sort33044
Quick sort261428
Heap sort513468

How to Use the Sorting Algorithm Visualizer

  1. 1. Choose an algorithm. Bubble, insertion, selection, merge, quick or heap sort. The page explains the one you pick and lists its time and space cost.
  2. 2. Give it an input. Type 2 to 40 numbers, or generate one: random order, nearly sorted, reversed, already sorted, or few unique values. The same seed always makes the same input, so you can repeat a run or share it.
  3. 3. Step through. Use Step forward and Step back to move one operation at a time, or Play to watch it run at a calm pace. A sentence under the bars says what just happened in words.
  4. 4. Compare. The table at the bottom shows how many comparisons, swaps and writes all six algorithms need for exactly your numbers.

Playback never flashes: the fastest setting changes the picture two and a half times a second at most. If your device is set to reduce motion, automatic playback is switched off and you step manually instead.

What the Counters Mean

  • Comparisons — each time the algorithm tests whether one value is greater than, equal to or less than another. This is the cost measure that sorting theory counts.
  • Swaps — each time two array elements exchange places. A swap is also two writes.
  • Array writes — every assignment to the array. Insertion sort shifts values instead of swapping them, and merge sort writes merged values back from a temporary copy, so those two show writes and no swaps.

The Six Algorithms Compared

Time and space cost of the six sorts as implemented here
AlgorithmBestAverageWorstExtra spaceStable
BubbleO(n)O(n²)O(n²)O(1)Yes
InsertionO(n)O(n²)O(n²)O(1)Yes
SelectionO(n²)O(n²)O(n²)O(1)No
MergeO(n log n)O(n log n)O(n log n)O(n)Yes
QuickO(n log n)O(n log n)O(n²)O(log n) to O(n)No
HeapO(n log n)O(n log n)O(n log n)O(1)No

Stable means equal values keep their original relative order — important when you sort records by one field after another. O(n²) means the work grows with the square of the list length, so doubling the list roughly quadruples it; O(n log n) grows only a little faster than the list itself. The best case for bubble sort assumes the usual early exit when a pass makes no swaps, which this version has.

Worked Example: Bubble Sort on 5, 1, 4, 2, 8

  1. Pass 1: compare 5 and 1 — swap; 5 and 4 — swap; 5 and 2 — swap; 5 and 8 — in order. The list is 1, 4, 2, 5, 8 and 8 is settled. Four comparisons, three swaps.
  2. Pass 2: compare 1 and 4 — in order; 4 and 2 — swap; 4 and 5 — in order. The list is 1, 2, 4, 5, 8. Three comparisons, one swap.
  3. Pass 3: compare 1 and 2, then 2 and 4 — both in order. No swaps, so the algorithm stops early. Two comparisons.

The total is 9 comparisons and 4 swaps (8 writes). Selection sort needs 10 comparisons and 2 swaps on the same numbers, and insertion sort 7 comparisons and 7 writes — the visualizer shows each of these when you select the algorithm.

Why the Input Changes the Cost

Algorithms with the same average cost can behave very differently on particular inputs. This table counts comparisons for 40 values; the random column is the Random order pattern with seed 2025, which you can regenerate in the widget.

Comparisons to sort 40 values
AlgorithmAlready sortedReversedRandom (seed 2025)
Bubble39780752
Insertion39780444
Selection780780780
Merge116100169
Quick780780168
Heap321278307

Bubble and insertion sort finish a sorted list in a single sweep of 39 comparisons, but pay 780 — all 40 × 39 ÷ 2 pairs — on a reversed one. Selection sort cannot tell and always pays 780. Merge sort is steady whatever the order. Quick sort here picks the last value as its pivot, so a sorted or reversed list gives the worst partitions and it also pays 780, while on random input it needs only 168. Real implementations choose pivots more carefully to avoid this.

Frequently Asked Questions

Which sorting algorithm is the fastest?
There is no single winner. For large random inputs, merge sort, quick sort and heap sort, all O(n log n), beat the O(n²) algorithms by a wide margin. For very short or nearly sorted lists, insertion sort is often fastest in practice because it does so little work. Production libraries combine methods, for example insertion sort for small pieces inside a merge or quick sort.
Why is bubble sort slow?
It only ever compares neighbours, so a value that is far from its place moves one position per pass. On a list of n values it can need about n²/2 comparisons and swaps. It is taught because it is easy to follow, not because it is used for real work.
What is the difference between a stable and an unstable sort?
A stable sort keeps equal values in their original order. If you sort people by city and then by surname with a stable sort, those with the same surname stay grouped by city. Bubble, insertion and merge sort are stable; selection, quick and heap sort, as written here, are not.
Why does quick sort have an O(n²) worst case yet get used so much?
The worst case needs the pivot to land at an extreme every time, which a good pivot rule, such as the median of three values or a random choice, makes very unlikely. On typical data it is faster than merge sort in practice because it sorts in place with little data movement.
What does O(n log n) mean?
It describes how the number of steps grows with the list length n. Sorting 1,000 values takes on the order of 10,000 operations (1,000 × about 10), whereas an O(n²) sort needs on the order of 1,000,000. The log n factor comes from halving the problem repeatedly.
Why does merge sort show writes but no swaps?
Merge sort copies a range into a temporary buffer and then writes the values back in order, one at a time, rather than exchanging elements in place. The visualizer counts each of those assignments as an array write and shows the temporary buffer as a second row of bars.

Related Tools

More in Data, Markup & Code

See all Data, Markup & Code