In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, bogosort
(also known as permutation sort and stupid sort) is a
sorting algorithm
In computer science, a sorting algorithm is an algorithm that puts elements of a List (computing), list into an Total order, order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending ...
based on the
generate and test paradigm. The function successively generates
permutation
In mathematics, a permutation of a set can mean one of two different things:
* an arrangement of its members in a sequence or linear order, or
* the act or process of changing the linear order of an ordered set.
An example of the first mean ...
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. The algorithm's name is a
portmanteau
In linguistics, a blend—also known as a blend word, lexical blend, or portmanteau—is a word formed by combining the meanings, and parts of the sounds, of two or more words together. of the words ''bogus'' and ''sort''.
Two versions of this algorithm exist: a deterministic version that enumerates all permutations until it hits a sorted one,
[.] and a
randomized version that randomly permutes its input and checks whether it is sorted. 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 unlikely to occur.
Description of the algorithm
Pseudocode
The following is a description of the randomized algorithm in
pseudocode
In computer science, pseudocode is a description of the steps in an algorithm using a mix of conventions of programming languages (like assignment operator, conditional operator, loop) with informal, usually self-explanatory, notation of actio ...
:
while deck is not sorted:
shuffle(deck)
C
An implementation in
C:
#include
#include
#include
// 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)
int is_sorted(int* a, int size)
void shuffle(int* a, int size)
int main()
Python
An implementation in
Python:
import random
# 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 < random_array - 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 andom.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 creates a random array - random_array - in generate_random_array that would be sorted by shuffling it in bogosort. All data in the array is natural numbers from 1 - 100.
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 .
[.] 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.
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
The infinite monkey theorem states that a monkey hitting keys independently and at randomness, random on a typewriter keyboard for an infinity, infinite amount of time will almost surely type any given text, including the complete works of Willi ...
holds: there is some probability of getting the right permutation, so given an unbounded number of tries it will
almost surely
In probability theory, an event is said to happen almost surely (sometimes abbreviated as a.s.) if it happens with probability 1 (with respect to the probability measure). In other words, the set of outcomes on which the event does not occur ha ...
eventually be chosen.
Related algorithms
See also
*
Las Vegas algorithm
In computing, a Las Vegas algorithm is a randomized algorithm that always gives Correctness (computer science), correct results; that is, it always produces the correct result or it informs about the failure. However, the runtime of a Las Vegas alg ...
*
Stooge sort
References
External links
*
BogoSort on
WikiWikiWeb
The WikiWikiWeb is the first wiki, or user-editable website. It was launched on 25 March 1995 by programmer Ward Cunningham and has been a read-only archive since 2015. The name ''WikiWikiWeb'' originally also applied to the wiki software that o ...
Inefficient sort algorithmsBogosort an implementation that runs on
Unix-like
A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
systems, similar to the standard
sort program.
Bogosortan
jmmcg::bogosort Simple, yet perverse, C++ implementations of the bogosort algorithm.
Bogosort NPM package bogosort implementation for Node.js ecosystem.
* Max Sherma
Bogo-sort is Sort of Slow June 2013
{{sorting
Comparison sorts
Articles with example Python (programming language) code