Search algorithmsCTRL K

Sorting

· 14 algorithms · bar visualizer
Start with Quick sort
// sorting

Sorting puts n values in order, and it is the most studied problem in computing because almost everything depends on it: searching, grouping, deduplicating, drawing.

The 12 algorithms here go from bubble sort, which everyone learns first, to quick sort and heap sort, which standard libraries build on. Each one spends comparisons and swaps differently; the bars make that visible.

Quick sort
quick sort · n=48 · Lomuto
comparingswappedpivotdone

All algorithms

14
Each card runs its own algorithm
Bubble sort
PLAY →

Adjacent swaps push the largest value to the end each pass.

AVGO(n²)
WORSTO(n²)
SPACEO(1)
Insertion sort
PLAY →

Takes each value and slides it left into the sorted prefix.

AVGO(n²)
WORSTO(n²)
SPACEO(1)
Selection sort
PLAY →

Finds the minimum of the rest and swaps it to the front.

AVGO(n²)
WORSTO(n²)
SPACEO(1)
Cocktail shaker
PLAY →

Bubble sort in both directions, so small values also move fast.

AVGO(n²)
WORSTO(n²)
SPACEO(1)
Gnome sort
PLAY →

One pointer walks forward and steps back on every inversion.

AVGO(n²)
WORSTO(n²)
SPACEO(1)
Comb sort
PLAY →

Bubble sort with a shrinking gap, which kills turtles early.

AVGO(n log n)
WORSTO(n²)
SPACEO(1)
Shell sort
PLAY →

Insertion sort over gapped subsequences, then a final gap of 1.

AVGO(n^1.3)
WORSTO(n²)
SPACEO(1)
Merge sort
PLAY →

Merges sorted runs of doubling width. Stable, needs a buffer.

AVGO(n log n)
WORSTO(n log n)
SPACEO(n)
Quick sort
PLAY →

Partitions around a pivot, then recurses on both sides.

AVGO(n log n)
WORSTO(n²)
SPACEO(log n)
Heap sort
PLAY →

Builds a max-heap, then pops the maximum to the end n times.

AVGO(n log n)
WORSTO(n log n)
SPACEO(1)
Odd-even sort
PLAY →

Alternates odd and even pairs; every pair in a phase is independent.

AVGO(n²)
WORSTO(n²)
SPACEO(1)
Radix sort (LSD)
PLAY →

Buckets by digit, least significant first. No comparisons at all.

AVGO(n · k)
WORSTO(n · k)
SPACEO(n + k)
Sleep sort
PLAY →

One timer per value; whoever wakes first is printed first. The scheduler does the sorting.

AVGO(max)
WORSTO(max)
SPACEO(n)
Bogo sort
PLAY →

Shuffle until it happens to be sorted. The joke every sorting video ends with.

AVGO(n · n!)
WORSTO(∞)
SPACEO(1)