sort.cny.shStart a race
Back to library
13 / 18Algorithm

Bogo Sort

Shuffle until sorted. Joke algorithm — capped to stay polite.

StableIn-placeUnstable
BestO(n)
AverageO((n+1)!)
Worstunbounded
01How it works

Check whether the array is sorted. If not, shuffle it uniformly at random and check again. Repeat forever — or, in this implementation, until a sanity cap trips and the player gives up.

02Best for

Jokes, teaching what "expected runtime" means, and illustrating the gulf between best-case and average-case analysis. Also a surprisingly decent stress test for random-number generators.

03Avoid when

Every other moment of your life. Expected time is O((n+1)!) — at n = 30 the expected runtime already outlives the heat death of the universe.