Английская Википедия:Bogosort
Шаблон:Short description Шаблон:Use dmy dates Шаблон:Infobox Algorithm
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>
// 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);
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 pseudocode rewritten in Python 3:
from random import shuffle
def is_sorted(data) -> bool:
"""Determine whether the data is sorted."""
return all(a <= b for a, b in zip(data, data[1:]))
def bogosort(data) -> list:
"""Shuffle data until sorted."""
while not is_sorted(data):
shuffle(data)
return data
This code assumes that Шаблон:Code is a simple, mutable, array-like data structure—like Python's built-in Шаблон:Code—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 Шаблон:Math, and the expected number of swaps in the average case equals Шаблон:Math.[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 Шаблон:Math, 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
Шаблон:Glossary Шаблон:Term Шаблон:Defn Шаблон:Term Шаблон:Defn Шаблон:Term Шаблон:Defn Шаблон:Term Шаблон:Defn Шаблон:Term Шаблон:Defn Шаблон:Term Шаблон:Defn Шаблон:Term Шаблон:Defn Шаблон:Glossary end
See also
Notes
References
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Шаблон: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
- ↑ 1,0 1,1 1,2 Шаблон:Citation.
- ↑ 2,0 2,1 Шаблон:Citation
- ↑ E. S. Raymond. "bogo-sort". The New Hacker’s Dictionary. MIT Press, 1996.
- ↑ Шаблон:Citation.
- ↑ Шаблон:Cite web