# Bogosort

Class Sorting algorithm Array Unbounded (randomized version),[1] O((n+1)!) (deterministic version) or O((∞)!) O(n)[1] O((n+1)!)[1] O(n)

In computer science, bogosort[1][2] (also permutation sort, stupid sort,[3] slowsort,[4] shotgun sort or monkey sort) is a highly ineffective sorting algorithm based on the generate and test paradigm. The algorithm successively generates permutations of its input until it finds one that is sorted. It is not useful for sorting, but may be used for educational purposes, to contrast it with other more realistic algorithms.

Two versions of the algorithm exist: a deterministic version that enumerates all permutations until it hits a sorted one,[2][4] and a randomized version that randomly permutes its input. An analogy for the working of the latter version is to sort a deck of cards by throwing the deck into the air, picking the cards up at random, and repeating the process until the deck is sorted. Its name comes from the word bogus.[5]

## Description of the algorithm

The following is a description of the randomized algorithm in pseudocode:

while not isInOrder(deck):
shuffle(deck)


## Running time and termination

If all elements to be sorted are distinct, the expected number of comparisons performed in the average case by randomized bogosort is asymptotically equivalent to ${\displaystyle (e-1)n!}$, and the expected number of swaps in the average case equals ${\displaystyle (n-1)n!}$.[1] The expected number of swaps grows faster than the expected number of comparisons, because if the elements are not in order, this will usually be discovered after only a few comparisons, no matter how many elements there are; but the work of shuffling the collection is proportional to its size. In the worst case, the number of comparisons and swaps are both unbounded, for the same reason that a tossed coin might turn up heads any number of times in a row.

The best case occurs if the list as given is already sorted; in this case the expected number of comparisons is ${\displaystyle n-1}$, and no swaps at all are carried out.[1]

For any collection of fixed size, the expected running time of the algorithm is finite for much the same reason that the infinite monkey theorem holds: there is some probability of getting the right permutation, so given an unbounded number of tries it will almost surely eventually be chosen.

## Related algorithms

Gorosort
is a sorting algorithm introduced in the 2011 Google Code Jam.[6] As long as the list is not in order, a subset of all elements is randomly permuted. If this subset is optimally chosen each time this is performed, the expected value of the total number of times this operation needs to be done is equal to the number of misplaced elements.
Bogobogosort
is an algorithm that was designed not to succeed before the heat death of the universe on any sizable list. It works by recursively calling itself with smaller and smaller copies of the beginning of the list to see if they are sorted. The best case is a single element, which is always sorted. For other cases, it compares the last element to the maximum element from the previous elements in the list. If the last element is greater or equal, it checks if the order of the copy matches the previous version, copies back if not, and returns. Otherwise, it reshuffles the current copy of the list and goes back to its recursive check.[7]
Bozosort
is another sorting algorithm based on random numbers. If the list is not in order, it picks two items at random and swaps them, then checks to see if the list is sorted. The running time analysis of a bozosort is more difficult, but some estimates are found in H. Gruber's analysis of "perversely awful" randomized sorting algorithms.[1] O(n!) is found to be the expected average case.
Worstsort
is a pessimal sorting algorithm that is guaranteed to complete, assuming that the universe does not burn itself out before completion of the algorithm; however, there is no computable limit to the inefficiency of the sorting algorithm, and therefore it is more pessimal than the other algorithms described herein. The ${\displaystyle worstsort}$ algorithm is based on a bad sorting algorithm, ${\displaystyle badsort}$. The badsort algorithm accepts two parameters: ${\displaystyle L}$, which is the list to be sorted, and ${\displaystyle k}$, which is a recursion depth. At recursion level ${\displaystyle k=0}$, ${\displaystyle badsort}$ merely uses a common sorting algorithm, such as bubblesort, to sort its inputs and return the sorted list. That is to say, ${\displaystyle badsort(L,0)=bubblesort(L)}$. Therefore, badsort's time complexity is ${\displaystyle O\left(n^{2}\right)}$ if ${\displaystyle k=0}$. However, for any ${\displaystyle k>0}$, ${\displaystyle badsort(L,k)}$ first generates ${\displaystyle P}$, the list of all permutations of ${\displaystyle L}$. Then, ${\displaystyle badsort}$ calculates ${\displaystyle badsort(P,k-1)}$, and returns the first element of the sorted ${\displaystyle P}$. To make ${\displaystyle worstsort}$ truly pessimal, ${\displaystyle k}$ may be assigned to the value of a computable increasing function such as ${\displaystyle f\colon \mathbb {N} \to \mathbb {N} }$ (e.g. ${\displaystyle f(n)=A(n,n)}$, where ${\displaystyle A}$ is Ackermann's function). Ergo, to sort a list arbitrarily badly, you would execute ${\displaystyle worstsort(L,f)=badsort(L,f(length(L)))}$, where ${\displaystyle length(L)}$ = number of elements in ${\displaystyle L}$. The resulting algorithm has complexity ${\displaystyle \Omega \left(\left(n!^{(f(n))}\right)^{2}\right)}$, where ${\displaystyle n!^{(m)}=(\dotso ((n!)!)!\dotso )!}$ = factorial of ${\displaystyle n}$ iterated ${\displaystyle m}$ times. This algorithm can be made as inefficient as we wish by picking a fast enough growing function ${\displaystyle f}$.[8]