Bogosort: Difference between revisions
→Related algorithms: Added content Tags: Reverted Mobile edit Mobile web edit |
Pythoncoder (talk | contribs) →Related algorithms: add bogobogosort anchor for redirect(s) |
||
(38 intermediate revisions by 27 users not shown) | |||
Line 34: | Line 34: | ||
}}</ref> (also known as '''permutation sort''' and '''stupid sort'''<ref>E. S. Raymond. "bogo-sort". ''The New Hacker’s Dictionary''. MIT Press, 1996.</ref>) is a [[sorting algorithm]] based on the [[generate and test]] paradigm. The function successively generates [[permutation]]s of its input until it finds one that is sorted. It is not considered useful for sorting, but may be used for educational purposes, to contrast it with more efficient algorithms. |
}}</ref> (also known as '''permutation sort''' and '''stupid sort'''<ref>E. S. Raymond. "bogo-sort". ''The New Hacker’s Dictionary''. MIT Press, 1996.</ref>) is a [[sorting algorithm]] based on the [[generate and test]] paradigm. The function successively generates [[permutation]]s of its input until it finds one that is sorted. It is not considered useful for sorting, but may be used for educational purposes, to contrast it with more efficient algorithms. |
||
Two versions of this algorithm exist: a deterministic version that enumerates all permutations until it hits a sorted one,<ref name="KSFS"/><ref name="Naish86">{{citation |last=Naish |first=Lee |title=Proceedings of the Third International Conference on Logic Programming |volume=225 |pages=624–634 |year=1986 |series=[[Lecture Notes in Computer Science]] |contribution=Negation and quantifiers in NU-Prolog |publisher=Springer-Verlag |doi=10.1007/3-540-16492-8_111}}.</ref> and a [[randomized algorithm|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. |
Two versions of this algorithm exist: a deterministic version that enumerates all permutations until it hits a sorted one,<ref name="KSFS"/><ref name="Naish86">{{citation |last=Naish |first=Lee |title=Proceedings of the Third International Conference on Logic Programming |volume=225 |pages=624–634 |year=1986 |series=[[Lecture Notes in Computer Science]] |contribution=Negation and quantifiers in NU-Prolog |publisher=Springer-Verlag |doi=10.1007/3-540-16492-8_111|isbn=978-3-540-16492-0 }}.</ref> and a [[randomized algorithm|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. In a worst-case scenario with this version, the random source is of low quality and happens to make the sorted permutation unboundedly unlikely to occur. The algorithm's name is a [[portmanteau]] of the words ''bogus'' and ''sort''.<ref>{{Cite web|title=bogosort|url=https://xlinux.nist.gov/dads/HTML/bogosort.html|access-date=2020-11-11|website=xlinux.nist.gov}}</ref> |
||
==Description of the algorithm== |
==Description of the algorithm== |
||
=== Pseudocode === |
|||
The following is a description of the randomized algorithm in [[pseudocode]]: |
The following is a description of the randomized algorithm in [[pseudocode]]: |
||
Line 42: | Line 44: | ||
shuffle(deck) |
shuffle(deck) |
||
=== C === |
|||
Here is the above pseudocode rewritten in [[Python (programming language)|Python 3]]: |
|||
Here is an implementation in C:<syntaxhighlight lang="c" line="1"> |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <time.h> |
|||
// executes in-place bogo sort on a given array |
|||
static void bogo_sort(int* a, int size); |
|||
// returns 1 if given array is sorted and 0 otherwise |
|||
static int is_sorted(int* a, int size); |
|||
// shuffles the given array into a random order |
|||
static void shuffle(int* a, int size); |
|||
void bogo_sort(int* a, int size) { |
|||
while (!is_sorted(a, size)) { |
|||
shuffle(a, size); |
|||
} |
|||
} |
|||
int is_sorted(int* a, int size) { |
|||
for (int i = 0; i < size-1; i++) { |
|||
if (a[i] > a[i+1]) { |
|||
return 0; |
|||
} |
|||
} |
|||
return 1; |
|||
} |
|||
void shuffle(int* a, int size) { |
|||
int temp, random; |
|||
for (int i = 0; i < size; i++) { |
|||
random = (int) ((double) rand() / ((double) RAND_MAX + 1) * size); |
|||
temp = a[random]; |
|||
a[random] = a[i]; |
|||
a[i] = temp; |
|||
} |
|||
} |
|||
int main() { |
|||
// example usage |
|||
int input[] = { 68, 14, 78, 98, 67, 89, 45, 90, 87, 78, 65, 74 }; |
|||
int size = sizeof(input) / sizeof(*input); |
|||
// initialize pseudo-random number generator |
|||
srand(time(NULL)); |
|||
bogo_sort(input, size); |
|||
// sorted result: 14 45 65 67 68 74 78 78 87 89 90 98 |
|||
printf("sorted result:"); |
|||
for (int i = 0; i < size; i++) { |
|||
printf(" %d", input[i]); |
|||
} |
|||
printf("\n"); |
|||
return 0; |
|||
} |
|||
</syntaxhighlight> |
|||
=== Python === |
|||
Here is the above python code written in [[Python (programming language)|Python 3]]: |
|||
<syntaxhighlight lang="python3"> |
<syntaxhighlight lang="python3"> |
||
import random |
|||
# bogosort |
|||
def is_sorted(data) -> bool: |
|||
# what happens is there is a random array that is generated by the last function |
|||
"""Determine whether the data is sorted.""" |
|||
# the first function checks whether the array is sorted or not |
|||
return all(a <= b for a, b in zip(data, data[1:])) |
|||
# the second function repeatedly shuffles the array for as long as it remains unsorted |
|||
# and that's it |
|||
# happy coding => |
|||
# this function checks whether or not the array is sorted |
|||
def bogosort(data) -> list: |
|||
def is_sorted(random_array): |
|||
"""Shuffle data until sorted.""" |
|||
for i in range(1, len(random_array)): |
|||
while not is_sorted(data): |
|||
if random_array[i] < random_array[i - 1]: |
|||
shuffle(data) |
|||
return |
return False |
||
return True |
|||
# this function repeatedly shuffles the elements of the array until they are sorted |
|||
def bogo_sort(random_array): |
|||
while not is_sorted(random_array): |
|||
random.shuffle(random_array) |
|||
return random_array |
|||
# this function generates an array with randomly chosen integer values |
|||
def generate_random_array(size, min_val, max_val): |
|||
return [random.randint(min_val, max_val) for _ in range(size)] |
|||
# the size, minimum value and maximum value of the randomly generated array |
|||
size = 10 |
|||
min_val = 1 |
|||
max_val = 100 |
|||
random_array = generate_random_array(size, min_val, max_val) |
|||
print("Unsorted array:", random_array) |
|||
sorted_arr = bogo_sort(random_array) |
|||
print("Sorted array:", sorted_arr) |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 72: | Line 157: | ||
| series = Lecture Notes in Computer Science |
| series = Lecture Notes in Computer Science |
||
| title = 4th International Conference on Fun with Algorithms, Castiglioncello, Italy, 2007 |
| title = 4th International Conference on Fun with Algorithms, Castiglioncello, Italy, 2007 |
||
| date = 2007 |
|||
| url = http://www.hermann-gruber.com/pdf/fun07-final.pdf |
| url = http://www.hermann-gruber.com/pdf/fun07-final.pdf |
||
| volume = 4475| isbn = 978-3-540-72913-6 |
|||
| volume = 4475}}.</ref> 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. |
|||
}}.</ref> 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 {{math|''n'' − 1}}, and no swaps at all are carried out.<ref name="Fun07"/> |
The best case occurs if the list as given is already sorted; in this case the expected number of comparisons is {{math|''n'' − 1}}, and no swaps at all are carried out.<ref name="Fun07"/> |
||
Line 82: | Line 169: | ||
{{glossary}}<!--{{term}} adds its argument as an element id, so explicit anchors are unnecessary--> |
{{glossary}}<!--{{term}} adds its argument as an element id, so explicit anchors are unnecessary--> |
||
{{term|Gorosort}} |
{{term|Gorosort}} |
||
{{defn|A sorting algorithm introduced in the 2011 [[Google Code Jam]].<ref>[https:// |
{{defn|A sorting algorithm introduced in the 2011 [[Google Code Jam]].<ref>[https://github.com/google/coding-competitions-archive/tree/main/codejam/2011/qualification_round/gorosort Google Code Jam 2011, Qualification Rounds, Problem D]</ref> 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.}} |
||
{{anchor|Bogobogosort}} |
|||
{{term|Bogobogosort}} |
{{term|Bogobogosort}} |
||
{{defn|An algorithm that recursively calls itself with smaller and smaller copies of the beginning of the list to see if they are sorted. The base 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, and if so returns. Otherwise, it reshuffles the current copy of the list and restarts its recursive check.<ref>[http://www.dangermouse.net/esoteric/bogobogosort.html Bogobogosort]</ref> |
{{defn|An algorithm that recursively calls itself with smaller and smaller copies of the beginning of the list to see if they are sorted. The base 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, and if so returns. Otherwise, it reshuffles the current copy of the list and restarts its recursive check.<ref>[http://www.dangermouse.net/esoteric/bogobogosort.html Bogobogosort]</ref> |
||
Line 90: | Line 178: | ||
<!--It also faces the same pseudo-random problems as bogosort—it may never terminate in a real-world implementation.-->}} |
<!--It also faces the same pseudo-random problems as bogosort—it may never terminate in a real-world implementation.-->}} |
||
{{term|Worstsort}} |
{{term|Worstsort}} |
||
{{defn|1=A pessimal |
{{defn|1=A [[wiktionary:pessimal|pessimal]] sorting algorithm that is guaranteed to complete in finite time; however, its efficiency can be arbitrarily bad, depending on its configuration. The {{math|worstsort}} algorithm is based on a bad sorting algorithm, {{math|badsort}}. The badsort algorithm accepts two parameters: {{mvar|L}}, which is the list to be sorted, and {{mvar|k}}, which is a recursion depth. At recursion level {{math|1={{var|k}} = 0}}, {{math|badsort}} merely uses a common sorting algorithm, such as [[Bubble sort|bubblesort]], to sort its inputs and return the sorted list. That is to say, {{math|1=badsort({{var|L}}, 0) = bubblesort({{var|L}})}}. Therefore, badsort's time complexity is {{math|O({{var|n}}{{sup|2}})}} if {{math|1={{var|k}} = 0}}. However, for any {{math|{{var|k}} > 0}}, {{math|badsort({{var|L}}, {{var|k}})}} first generates {{mvar|P}}, the list of all permutations of {{mvar|L}}. Then, {{math|badsort}} calculates {{math|badsort({{var|P}}, {{var|k}} − 1)}}, and returns the first element of the sorted {{mvar|P}}. To make {{math|worstsort}} truly pessimal, {{mvar|k}} may be assigned to the value of a computable increasing function such as <math>f\colon\N \to \N</math> (e.g. {{math|1={{var|f}}({{var|n}}) = {{var|A}}({{var|n}}, {{var|n}})}}, where {{mvar|A}} is [[Ackermann's function]]). Therefore, to sort a list arbitrarily badly, one would execute {{math|1=worstsort({{var|L}}, {{var|f}}) = badsort({{var|L}}, {{var|f}}(length({{var|L}})))}}, where {{math|length({{var|L}})}} is the number of elements in {{mvar|L}}. The resulting algorithm has complexity <math display="inline">\Omega\left(\left(n!^{(f(n))}\right)^2\right)</math>, where <math>n!^{(m)} = (\dotso((n!)!)!\dotso)!</math> = factorial of {{mvar|n}} iterated {{mvar|m}} times. This algorithm can be made as inefficient as one wishes by picking a fast enough growing function {{mvar|f}}.<ref>{{Cite arXiv |eprint = 1406.1077|last1 = Lerma|first1 = Miguel A.|title = How inefficient can a sort algorithm be?|class = cs.DS|year = 2014}}</ref>}} |
||
{{term|[[Slowsort]]}} |
{{term|[[Slowsort]]}} |
||
{{defn|A different humorous sorting algorithm that employs a misguided divide-and-conquer strategy to achieve massive complexity.}} |
{{defn|A different humorous sorting algorithm that employs a misguided divide-and-conquer strategy to achieve massive complexity.}} |
||
{{term|Quantum bogosort}} |
{{term|Quantum bogosort}} |
||
{{defn|A hypothetical sorting algorithm based on bogosort, created as an [[in-joke]] among computer scientists. The algorithm generates a random permutation of its input using a quantum source of entropy, checks if the list is sorted, and, if it is not, destroys the universe. Assuming that the [[many-worlds interpretation]] holds, the use of this algorithm will result in at least one surviving universe where the input was successfully sorted in {{math|O({{var|n}})}} time.<ref>{{cite journal |author=The Other Tree |date=23 October 2009 |title=Quantum Bogosort |url=https://mathnews.uwaterloo.ca/wp-content/uploads/2014/08/v111i3-compressed.pdf#page=13 |journal= |
{{defn|A hypothetical sorting algorithm based on bogosort, created as an [[in-joke]] among computer scientists. The algorithm generates a random permutation of its input using a quantum source of entropy, checks if the list is sorted, and, if it is not, destroys the universe. Assuming that the [[many-worlds interpretation]] holds, the use of this algorithm will result in at least one surviving universe where the input was successfully sorted in {{math|O({{var|n}})}} time.<ref>{{cite journal |author=The Other Tree |date=23 October 2009 |title=Quantum Bogosort |url=https://mathnews.uwaterloo.ca/wp-content/uploads/2014/08/v111i3-compressed.pdf#page=13 |archive-url=https://web.archive.org/web/20200705214046/http://mathnews.uwaterloo.ca/wp-content/uploads/2014/08/v111i3-compressed.pdf |archive-date=2020-07-05 |url-status=live |journal=MathNEWS |volume=111 |issue=3 |page=13 |access-date=20 March 2022}}</ref>}} |
||
{{term|Miracle sort}} |
{{term|Miracle sort}} |
||
{{defn|A sorting algorithm that checks if the array is sorted until a [[miracle]] occurs. It continually checks the array until it is sorted, never changing the order of the array.<ref>{{Cite web |title=Miracle Sort |work=The Computer Science Handbook |url=https://www.thecshandbook.com/Miracle_Sort |access-date=2022-09-09}}</ref> Because the order is never altered, the algorithm has a hypothetical time complexity of {{math|''O''(''∞'')}}, but it can still sort through events such as miracles or [[single-event upset]]s. Particular care must be taken in the implementation of this algorithm as [[optimizing compiler]]s may simply transform it into a [[while(true)]] loop. However, the best case is {{math|''O''(''n'')}}, which happens on a sorted list. Since it only makes comparisons, it is both strictly in-place and stable.}} |
{{defn|A sorting algorithm that checks if the array is sorted until a [[miracle]] occurs. It continually checks the array until it is sorted, never changing the order of the array.<ref>{{Cite web |title=Miracle Sort |work=The Computer Science Handbook |url=https://www.thecshandbook.com/Miracle_Sort |access-date=2022-09-09}}</ref> Because the order is never altered, the algorithm has a hypothetical time complexity of {{math|''O''(''∞'')}}, but it can still sort through events such as miracles or [[single-event upset]]s. Particular care must be taken in the implementation of this algorithm as [[optimizing compiler]]s may simply transform it into a [[while(true)]] loop. However, the best case is {{math|''O''(''n'')}}, which happens on a sorted list. Since it only makes comparisons, it is both strictly in-place and stable.}} |
||
{{term|Bozobogo sort}} |
|||
{{defn|A sorting algorithm that only works if the list is already in order, otherwise, the conditions of miracle sort are applied.}} |
|||
Gaslighting sort: |
|||
This algorithm returns true regardless of the input. It has w.c. O(1) time complexity, and is one of the few algorithms that are both in place, and can get the array as const. |
|||
The name is was suggested by B. Vaisman in early days, due to the fact that the algorithm "gaslights" us. |
|||
{{glossary end}} |
{{glossary end}} |
||
Line 106: | Line 192: | ||
* [[Las Vegas algorithm]] |
* [[Las Vegas algorithm]] |
||
* [[Stooge sort]] |
* [[Stooge sort]] |
||
==Notes== |
|||
{{notelist}} |
|||
==References== |
==References== |
||
{{reflist}} |
{{reflist}} |
||
Line 124: | Line 206: | ||
[[Category:Comparison sorts]] |
[[Category:Comparison sorts]] |
||
[[Category:Impractical sorting algorithms]] |
|||
[[Category:Computer humor]] |
|||
[[Category:Articles with example Python (programming language) code]] |
[[Category:Articles with example Python (programming language) code]] |
Revision as of 21:31, 25 September 2024
Class | Sorting |
---|---|
Data structure | Array |
Worst-case performance | Unbounded (randomized version), (deterministic version) |
Best-case performance | [1] |
Average performance | [1] |
Worst-case space complexity |
In computer science, bogosort[1][2] (also known as permutation sort and stupid sort[3]) is a sorting algorithm based on the generate and test paradigm. The function successively generates permutations of its input until it finds one that is sorted. It is not considered useful for sorting, but may be used for educational purposes, to contrast it with more efficient algorithms.
Two versions of this 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. In a worst-case scenario with this version, the random source is of low quality and happens to make the sorted permutation unboundedly unlikely to occur. The algorithm's name is a portmanteau of the words bogus and sort.[5]
Description of the algorithm
Pseudocode
The following is a description of the randomized algorithm in pseudocode:
while not sorted(deck): shuffle(deck)
C
Here is an implementation in C:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// executes in-place bogo sort on a given array
static void bogo_sort(int* a, int size);
// returns 1 if given array is sorted and 0 otherwise
static int is_sorted(int* a, int size);
// shuffles the given array into a random order
static void shuffle(int* a, int size);
void bogo_sort(int* a, int size) {
while (!is_sorted(a, size)) {
shuffle(a, size);
}
}
int is_sorted(int* a, int size) {
for (int i = 0; i < size-1; i++) {
if (a[i] > a[i+1]) {
return 0;
}
}
return 1;
}
void shuffle(int* a, int size) {
int temp, random;
for (int i = 0; i < size; i++) {
random = (int) ((double) rand() / ((double) RAND_MAX + 1) * size);
temp = a[random];
a[random] = a[i];
a[i] = temp;
}
}
int main() {
// example usage
int input[] = { 68, 14, 78, 98, 67, 89, 45, 90, 87, 78, 65, 74 };
int size = sizeof(input) / sizeof(*input);
// initialize pseudo-random number generator
srand(time(NULL));
bogo_sort(input, size);
// sorted result: 14 45 65 67 68 74 78 78 87 89 90 98
printf("sorted result:");
for (int i = 0; i < size; i++) {
printf(" %d", input[i]);
}
printf("\n");
return 0;
}
Python
Here is the above python code written in Python 3:
import random
# bogosort
# what happens is there is a random array that is generated by the last function
# the first function checks whether the array is sorted or not
# the second function repeatedly shuffles the array for as long as it remains unsorted
# and that's it
# happy coding =>
# this function checks whether or not the array is sorted
def is_sorted(random_array):
for i in range(1, len(random_array)):
if random_array[i] < random_array[i - 1]:
return False
return True
# this function repeatedly shuffles the elements of the array until they are sorted
def bogo_sort(random_array):
while not is_sorted(random_array):
random.shuffle(random_array)
return random_array
# this function generates an array with randomly chosen integer values
def generate_random_array(size, min_val, max_val):
return [random.randint(min_val, max_val) for _ in range(size)]
# the size, minimum value and maximum value of the randomly generated array
size = 10
min_val = 1
max_val = 100
random_array = generate_random_array(size, min_val, max_val)
print("Unsorted array:", random_array)
sorted_arr = bogo_sort(random_array)
print("Sorted array:", sorted_arr)
This code assumes that data
is a simple, mutable, array-like data structure—like Python's built-in list
—whose elements can be compared without issue.
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 (e − 1)n!, and the expected number of swaps in the average case equals (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 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
- 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
- An algorithm that recursively calls itself with smaller and smaller copies of the beginning of the list to see if they are sorted. The base 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, and if so returns. Otherwise, it reshuffles the current copy of the list and restarts its recursive check.[7]
- Bozosort
- 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
- A pessimal sorting algorithm that is guaranteed to complete in finite time; however, its efficiency can be arbitrarily bad, depending on its configuration. The worstsort algorithm is based on a bad sorting algorithm, badsort. The badsort algorithm accepts two parameters: L, which is the list to be sorted, and k, which is a recursion depth. At recursion level k = 0, badsort merely uses a common sorting algorithm, such as bubblesort, to sort its inputs and return the sorted list. That is to say, badsort(L, 0) = bubblesort(L). Therefore, badsort's time complexity is O(n2) if k = 0. However, for any k > 0, badsort(L, k) first generates P, the list of all permutations of L. Then, badsort calculates badsort(P, k − 1), and returns the first element of the sorted P. To make worstsort truly pessimal, k may be assigned to the value of a computable increasing function such as (e.g. f(n) = A(n, n), where A is Ackermann's function). Therefore, to sort a list arbitrarily badly, one would execute worstsort(L, f) = badsort(L, f(length(L))), where length(L) is the 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 one wishes by picking a fast enough growing function f.[8]
- Slowsort
- A different humorous sorting algorithm that employs a misguided divide-and-conquer strategy to achieve massive complexity.
- Quantum bogosort
- A hypothetical sorting algorithm based on bogosort, created as an in-joke among computer scientists. The algorithm generates a random permutation of its input using a quantum source of entropy, checks if the list is sorted, and, if it is not, destroys the universe. Assuming that the many-worlds interpretation holds, the use of this algorithm will result in at least one surviving universe where the input was successfully sorted in O(n) time.[9]
- Miracle sort
- A sorting algorithm that checks if the array is sorted until a miracle occurs. It continually checks the array until it is sorted, never changing the order of the array.[10] Because the order is never altered, the algorithm has a hypothetical time complexity of O(∞), but it can still sort through events such as miracles or single-event upsets. Particular care must be taken in the implementation of this algorithm as optimizing compilers may simply transform it into a while(true) loop. However, the best case is O(n), which happens on a sorted list. Since it only makes comparisons, it is both strictly in-place and stable.
- Bozobogo sort
- A sorting algorithm that only works if the list is already in order, otherwise, the conditions of miracle sort are applied.
See also
References
- ^ a b c d e f Gruber, H.; Holzer, M.; Ruepp, O. (2007), "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, ISBN 978-3-540-72913-6.
- ^ 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, S2CID 1435535, archived from the original (PDF) on 26 March 2012, retrieved 22 June 2011
- ^ E. S. Raymond. "bogo-sort". The New Hacker’s Dictionary. MIT Press, 1996.
- ^ 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, ISBN 978-3-540-16492-0.
- ^ "bogosort". xlinux.nist.gov. Retrieved 11 November 2020.
- ^ Google Code Jam 2011, Qualification Rounds, Problem D
- ^ Bogobogosort
- ^ Lerma, Miguel A. (2014). "How inefficient can a sort algorithm be?". arXiv:1406.1077 [cs.DS].
- ^ The Other Tree (23 October 2009). "Quantum Bogosort" (PDF). MathNEWS. 111 (3): 13. Archived (PDF) from the original on 5 July 2020. Retrieved 20 March 2022.
- ^ "Miracle Sort". The Computer Science Handbook. Retrieved 9 September 2022.
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[permanent dead link]: Simple, yet perverse, C++ implementations of the bogosort algorithm.
- Bogosort NPM package: bogosort implementation for Node.js ecosystem.
- Max Sherman Bogo-sort is Sort of Slow, June 2013