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

Miracle Sort

Just for fun: check if it's sorted; if not, wait for cosmic rays.

StableIn-placeUnstable
BestO(n)
Averageunbounded
Worstunbounded
01How it works

Check whether the array is already sorted. If it is, return. If it isn't, do nothing and check again — relying on cosmic rays, hardware faults, or divine intervention to flip the right bits. Capped to a small number of "prayers" before giving up.

02Best for

Reminding interviewers that "O(1) extra memory" is not the strongest property an algorithm can have.

03Avoid when

Always. The expected runtime exceeds the lifespan of the universe by several orders of magnitude.