Bogosort
Class | Sorting algorithm |
---|---|
Data structure | Array |
Worst-case performance | Unbounded (randomized version),[1] O((n+1)!) (deterministic version) |
Best-case performance | θ(n)[1] |
Average performance | O((n+1)!)[1] |
Worst-case space complexity | O(n) |
Optimal | No |
In computer science, bogosort[1][2] (also permutation sort, stupid sort,[3] slowsort,[4] shotgun sort or monkey sort) is a particularly 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 , and the expected number of swaps in the average case equals .[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 , 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
This article is missing information about quantum bogosort.(November 2015) |
- 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
- designed as the final answer to how inefficient a sorting algorithm can be. It is based on a multilevel sorting algorithm that works as follows. At level 0, multilevelsort is just any classical sorting algorithm such a bubble sort: multilevelsort(L,0) = bublesort(L). Next, for any k > 0, multilevelsort(L,k) generates P = list of all permutations of L, runs multilevelsort(P,k-1), and copies the first element of the sorted P into L. Finally, worstsort accepts two arguments, the list L to be sorted, and a computable increasing function (e.g. Ackermann's function), then it executes worstsort(L,f) = multilevelsort(L,f(length(L))), where length(L) = number of elements in L. The resulting algorithm has complexity , where = factorial of n iterated m times. This algorithm can be made as inefficient as we wish by picking a fast enough growing function f.[8]
See also
References
- ^ a b c d e f g Gruber, H.; Holzer, M.; Ruepp, O., "Sorting the slow way: an analysis of perversely awful randomized sorting algorithms", 4th International Conference on Fun with Algorithms, Castiglioncello, Italy, 2007 (PDF), Lecture Notes in Computer Science, vol. 4475, Springer-Verlag, pp. 183–197, doi:10.1007/978-3-540-72914-3_17.
- ^ a b Kiselyov, Oleg; Shan, Chung-chieh; Friedman, Daniel P.; Sabry, Amr (2005), "Backtracking, interleaving, and terminating monad transformers: (functional pearl)", Proceedings of the Tenth ACM SIGPLAN International Conference on Functional Programming (ICFP '05) (PDF), SIGPLAN Notices, pp. 192–203, doi:10.1145/1086365.1086390
- ^ E. S. Raymond. "bogo-sort". The New Hacker’s Dictionary. MIT Press, 1996.
- ^ a b Naish, Lee (1986), "Negation and quantifiers in NU-Prolog", Proceedings of the Third International Conference on Logic Programming, Lecture Notes in Computer Science, vol. 225, Springer-Verlag, pp. 624–634, doi:10.1007/3-540-16492-8_111.
- ^ "Bogosort". The Jargon File 4.4.8. 2003. Retrieved 11 April 2013.
- ^ Google Code Jam 2011, Qualification Rounds, Problem D
- ^ Bogobogosort
- ^ How inefficient can a sort algorithm be?
External links
- BogoSort on WikiWikiWeb
- Inefficient sort algorithms
- Bogosort: an implementation that runs on Unix-like systems, similar to the standard sort program.
- Bogosort and jmmcg::bogosort: Simple, yet perverse, C++ implementations of the bogosort algorithm.