Watch seven classic sorting algorithms rearrange an array bar by bar. Follow the highlighted pseudocode line, count comparisons, swaps and writes, slow down to single steps, or race all seven on the same data to see Big-O complexity in action.
Last updated:
Comparisons0
Swaps0
Array writes0
Steps0
StatusReady
ComparingSwap / writePivot / keyIn final place
Use my own numbers
Up to 250 non-negative whole numbers (0 to 9999), separated by commas or spaces.
Advertisement
Advertisement
How to use the Sorting Algorithm Visualizer
Pick an algorithm and the kind of starting data (random, nearly sorted, reversed or few unique values).
Set the array size and speed with the sliders. Small arrays show each value above its bar.
Press Start to animate, Step (or the right arrow key) to advance one operation, or Finish instantly to see the final counts.
Follow along in the pseudocode panel: the current line is highlighted and the sentence below explains the operation.
Press Race all algorithms to run every algorithm on the same array and compare how many steps each one needs.
Sorting algorithm complexity cheat sheet
Algorithm
Best
Average
Worst
Extra space
Stable
Good for
Bubble sort
O(n)
O(n²)
O(n²)
O(1)
Yes
Teaching; detecting an already sorted array
Insertion sort
O(n)
O(n²)
O(n²)
O(1)
Yes
Small or nearly sorted arrays, online input
Selection sort
O(n²)
O(n²)
O(n²)
O(1)
No
Minimizing swaps (at most n − 1)
Merge sort
O(n log n)
O(n log n)
O(n log n)
O(n)
Yes
Guaranteed speed, linked lists, external sorting
Quick sort
O(n log n)
O(n log n)
O(n²)
O(log n)
No
Fast general-purpose in-memory sorting
Heap sort
O(n log n)
O(n log n)
O(n log n)
O(1)
No
Worst-case guarantee without extra memory
Radix sort (LSD)
O(d·(n+b))
O(d·(n+b))
O(d·(n+b))
O(n+b)
Yes
Integers or fixed-length keys
n is the number of items, d the number of digits in the largest key and b the base (10 here). Any sort that only compares items needs at least about log₂(n!) ≈ n·log₂(n) − 1.44n comparisons in the worst case, which is why O(n log n) is the best a comparison sort can do.
Experiments worth trying
Nearly sorted data: run insertion sort and bubble sort, then quick sort. The simple algorithms finish in close to n steps while Lomuto quick sort slows to its worst case.
Few unique values: radix sort needs the same number of passes regardless of order, while quick sort with this partition scheme makes many redundant comparisons on duplicate keys.
Count the swaps: selection sort makes at most n − 1 swaps but always n(n − 1)/2 comparisons; bubble sort can make up to n(n − 1)/2 swaps.
Double the size: going from 50 to 100 items roughly quadruples the comparisons of O(n²) algorithms but only slightly more than doubles them for merge and heap sort.
Frequently asked questions
Which sorting algorithm is the fastest?
For general data, O(n log n) algorithms such as quick sort, merge sort and heap sort beat O(n²) ones like bubble, insertion and selection sort once arrays have more than a few dozen items. Quick sort is usually fastest in practice thanks to cache-friendly in-place partitioning, but its worst case is O(n²). Insertion sort wins on tiny or nearly sorted arrays, which is why library sorts such as Timsort and introsort switch to it for small pieces.
Why does quick sort get slow on sorted or reversed data here?
This visualizer uses the textbook Lomuto partition with the last element as the pivot. On data that is already sorted or reversed, that pivot is always the smallest or largest value, so each partition only removes one element and the run takes about n²/2 comparisons. Real libraries avoid this with random or median-of-three pivots.
What is a stable sort?
A stable sort keeps items with equal keys in their original relative order. Bubble, insertion, merge and radix sort are stable; selection, quick and heap sort are not. Stability matters when you sort by one field after another, such as sorting by name and then by department.
How can radix sort make zero comparisons?
Radix sort never compares two values. It distributes numbers into ten buckets by one digit at a time, starting from the ones place, using a stable counting pass. Its running time is O(d·(n + b)) for d digits and base b, so for fixed-width integers it can beat comparison sorts, whose lower bound is about n·log₂(n) comparisons.
What counts as a step?
One step is one elementary operation: comparing two items, swapping two items, writing a value into the array, or (for radix sort) reading a digit. A swap counts as two array writes. Steps are what the speed slider controls and what the race compares, so they reflect work done rather than wall-clock time on your computer.
What do the colors mean?
Amber bars are being compared or read, red bars are being swapped or written, purple marks the pivot or the current key or minimum, and dark green bars have reached their final sorted position. In merge sort the shaded band shows the part of the array being merged.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.