Bogosort
From Free net encyclopedia
Template:Wikisource According to the Jargon File, bogo-sort is "the archetypal perversely awful algorithm", one example of which is attempting to sort a deck of cards by repeatedly throwing the deck in the air, picking the cards up at random, and then testing whether the cards are in sorted order. It is named after the humorous term quantum bogodynamics and ultimately, the word bogus. Other names are stupid sort, bozo sort, blort sort, monkey sort, Stochastic sort and Drunk man sort.
Here is the pseudocode of the algorithm:
function bogosort(array) while not is_sorted(array) array := random_permutation(array)
This sorting algorithm is probabilistic in nature. If all elements to be sorted are distinct, the expected complexity is O(n × n!). The exact expected running time depends on how many different element values occur, and how often each of them occurs, but for non-trivial cases the expected running time is exponential or super-exponential in n. It terminates for the same reason that the infinite monkey theorem holds; there is some probability of getting the right permutation, so given an infinite number of tries it must eventually find it.
It should be noted that with real-world pseudo-random number algorithms, which have a finite number of states and are not in any case actually random, the algorithm may never terminate for certain inputs.
Bogosort is not a stable sort (unless you're superbly lucky).
Caveat
If you use the same random number algorithm and the same starting seed to sort the array as was used to randomize the array, you can be surprised by successfully sorting in one iteration. This is caused by the failure of a computer to produce random numbers. Do not expect such a quick resolution with true random numbers or another seed.
Bozo sort
A horrible and most inefficient sorting algorithm that sorts a list by picking two random items in the list, swapping them, and checking if the list is now in order. Bozosort is not to be confused with Bogosort, however. Bogosort tests if a list is in order, then tries a completely different random permutation. Bozosort is even more inefficient because it only switches two items in the list at a time. For an explanation of why bozo sort will almost surely succeed, see Infinite Monkey Theorem.
External links
es:Stupid sort nl:Bogosort pl:Bogosort pt:Bogosort zh:Bogo排序