Bogosort Explained

Class:Sorting
Data:Array
Time:Unbounded (randomized version),

\Omicron(n x n!)

(deterministic version)
Average-Time:

\Theta(n x n!)

Best-Time:

\Omega(n)

Space:

\Omicron(1)

In computer science, bogosort (also known as permutation sort and stupid sort[1]) 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] 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.[3]

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:

  1. include
  2. include
  3. include

// executes in-place bogo sort on a given arraystatic void bogo_sort(int* a, int size);// returns 1 if given array is sorted and 0 otherwisestatic int is_sorted(int* a, int size);// shuffles the given array into a random orderstatic 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

Here is the above pseudocode rewritten in Python 3:

import random

  1. bogosort
  2. what happens is there is a random array that is generated by the last function
  3. the first function checks whether the array is sorted or not
  4. the second function repeatedly shuffles the array for as long as it remains unsorted
  5. and that's it
  6. happy coding =>
  7. 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

  1. 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

  1. 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)]

  1. the size, minimum value and maximum value of the randomly generated array

size = 10min_val = 1max_val = 100random_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 is a simple, mutable, array-like data structure—like Python's built-in —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, and the expected number of swaps in the average case equals .[4] 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.[4]

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.

See also

External links

Notes and References

  1. E. S. Raymond. "bogo-sort". The New Hacker’s Dictionary. MIT Press, 1996.
  2. .
  3. Web site: bogosort. 2020-11-11. xlinux.nist.gov.
  4. .