Bogo sort applies generate-and-test without useful guidance: check whether the array is sorted; if not, shuffle it and try again. For n distinct values only one of the n! permutations is sorted, so a random attempt succeeds with probability 1/n!. A sequence of random shuffles can miss indefinitely.

The algorithm is a counterexample, not a production choice. It makes the value of progress invariants concrete: a real sorting algorithm proves that each step shrinks disorder or fixes a region, while Bogo Sort forgets all previous work after every shuffle.

Visualization

The teaching trace enumerates a deterministic bounded sequence of permutations instead of depending on random luck. It accepts at most five items and stops after at most 120 attempts. Those limits keep the card reproducible and finite; the classical algorithm remains random and has no finite worst-case bound.

Complexity
n
number of distinct elements in the array
  • Expected: Θ(n · n!)
  • Worst: Unbounded without an attempt cap

Boundary and implementation

The storage result shown above assumes an unbiased Fisher–Yates shuffle that modifies the array directly. An attempt cap makes a program terminate but changes the contract: it may return failure with an unsorted array. The deterministic StepTrace demonstrates the state space; it is not evidence that random Bogo Sort finishes within 120 attempts.

Questions

References