Skip to content
StackDecode
SortingAnimated

Bogosort

Shuffle until sorted. A joke algorithm that shows why efficiency matters.

Time

O(n · n!) expected

Extra space

O(1)

Use it for

Never.

How it works

  1. Check whether the list is sorted. If not, shuffle it randomly and check again.
  2. Repeat until it happens to be sorted.
  3. Expected shuffles ≈ n!: hopeless beyond a handful of items. It exists to show why a bad algorithm is bad.
  4. This demo uses a fixed random seed so it is repeatable, and stops after 60 shuffles.

Watch it run

Loading animation…