SortLab

Curiosity cabinet · Beginner

Bogo Sort

Shuffle randomly until the sequence happens to be sorted.

Best
O(n)
Average
O(n × n!)
Worst
Unbounded
Space
O(1)
Stability
Not stable
Memory model
In-place

How it builds order

  1. Check whether the list is sorted.
  2. If not, shuffle uniformly.
  3. Check again.
  4. Eventually get lucky—with no useful worst-case bound.

Why it works

Every ordering is a possible shuffle; one of those n! permutations is sorted.

Watch for: The demo uses at most five items because expected work grows factorially.

The interactive version of this page adds the full trace, synchronized pseudocode, practice decisions, input experiments, and mastery review.

Compare related strategies