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

Quantum Bogosort

Just for fun: shuffle once, destroy the universe if it isn't sorted.

StableIn-placeUnstable
BestO(n)
AverageO(n)
WorstO(n) — in the surviving branch
01How it works

Shuffle the array uniformly at random, then check if it's sorted. If it is, return. If it isn't, destroy the current universe — leaving only the surviving branches in which the shuffle happened to land sorted. Strictly a thought experiment that requires the many-worlds interpretation of quantum mechanics to actually work.

02Best for

Cocktail-party physics jokes and convincing your CS friends that complexity classes are negotiable if you have a big enough multiverse.

03Avoid when

Production. Also any universe you happen to be fond of — the algorithm relies on annihilating most of them.