# Bogosort

Class Sorting Array Unbounded (randomized version), O((n+1)!) (deterministic version) O(n) O((n+1)!) O(n)

In computer science, bogosort (also known as permutation sort, stupid sort, slowsort, shotgun sort, or monkey sort) is a highly inefficient 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 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, 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 is a portmanteau of the words bogus and sort.

## Description of the algorithm

The following is a description of the randomized algorithm in pseudocode:

while not isInOrder(deck):
shuffle(deck)


Here is the above pseudocode re-written in Python 3:

import random

def is_sorted(data) -> bool:
"""Determine whether the data is sorted."""
for i in range(len(data) - 1):
if data[i] > data[i + 1]:
return False
return True

def bogosort(data) -> List:
"""Shuffle data until sorted."""
while not is_sorted(data):
random.shuffle(data)
return data


This code assumes that data is a simple, mutable datatype—like Python's built-in list—whose elements can be compared without issue.

Here is an example with shuffle in Standard ML:

 val _ = load "Random";
val rng = Random.newgen ();

fun select (y::xs, 0) = (y, xs)
| select (x::xs, i) = let val (y, xs') = select (xs, i-1) in (y, x::xs') end
| select (_, i) = raise Fail ("Short by " ^ Int.toString i ^ " elements.");

(* Recreates a list in random order by removing elements in random positions *)
fun shuffle xs =
let fun rtake [] _ = []
| rtake ys max =
let val (y, ys') = select (ys, Random.range (0, max) rng)
in y :: rtake ys' (max-1)
end
in rtake xs (length xs) end;

fun bogosort xs comp =
let fun isSorted (x::y::xs) comp = comp(x,y) <> GREATER andalso isSorted (y::xs) comp
| isSorted _ comp = true;
val a = ref xs;
in while(not(isSorted (!a) comp)) do (
a := shuffle (!a)
); (!a) end;


## 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!$ . 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.

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
is a sorting algorithm introduced in the 2011 Google Code Jam. 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.
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. O(n!) is found to be the expected average case.
Worstsort
is a pessimal sorting algorithm that is guaranteed to complete in finite time; however, there is no computable limit to the inefficiency of the sorting algorithm, and therefore it is more pessimal than the other algorithms described herein. The ${\text{worstsort}}$ algorithm is based on a bad sorting algorithm, ${\text{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$ , ${\text{badsort}}$ merely uses a common sorting algorithm, such as bubblesort, to sort its inputs and return the sorted list. That is to say, ${\text{badsort}}(L,0)={\text{bubblesort}}(L)$ . Therefore, badsort's time complexity is $O\left(n^{2}\right)$ if $k=0$ . However, for any $k>0$ , ${\text{badsort}}(L,k)$ first generates $P$ , the list of all permutations of $L$ . Then, ${\text{badsort}}$ calculates ${\text{badsort}}(P,k-1)$ , and returns the first element of the sorted $P$ . To make ${\text{worstsort}}$ truly pessimal, $k$ may be assigned to the value of a computable increasing function such as $f\colon \mathbb {N} \to \mathbb {N}$ (e.g. $f(n)=A(n,n)$ , where $A$ is Ackermann's function). Ergo, to sort a list arbitrarily badly, you would execute ${\text{worstsort}}(L,f)={\text{badsort}}(L,f({\text{length}}(L)))$ , where ${\text{length}}(L)$ = number of elements in $L$ . The resulting algorithm has complexity $\Omega \left(\left(n!^{(f(n))}\right)^{2}\right)$ , where $n!^{(m)}=(\dotso ((n!)!)!\dotso )!$ = factorial of $n$ iterated $m$ times. This algorithm can be made as inefficient as we wish by picking a fast enough growing function $f$ .

## Analysis of runtime

Here is some Python code to efficiently test the average complexity of Bogosort.

#!/usr/bin/env python3.6

import sys
import time
import random
from multiprocessing import Process, Queue

import numpy as np
import matplotlib.pyplot as plt
from scipy.special import factorial
from tqdm import tqdm

WORKER_COUNT = 8
TRIAL_COUNT = 10

def is_sorted(some_list):
"""Determine whether the specified list is sorted."""
for x, y in zip(some_list[:-1], some_list[1:]):
if x > y:
return False
return True

class Sorter(Process):
def __init__(self, array, output: Queue, counts: Queue, *args, **kwargs):
super().__init__(*args, **kwargs)
self.array = array
self.length = len(array)
self.output = output
self.count = 0
self.counts = counts

def run(self) -> None:
while True:
if self.output.empty():
new_list = random.sample(self.array, k=len(self.array))
self.count += self.length  # To check all items
if is_sorted(new_list):
self.counts.put(self.count)
self.output.put(new_list)
break
else:
self.counts.put(self.count)
break

def run_trial(list_len):
trials = {"time": [], "cycles": []}

for _ in tqdm(range(TRIAL_COUNT)):
start_time = time.time()
array = random.sample(list(range(list_len)), k=list_len)

workers = []
output = Queue()
counts = Queue()
for _ in range(WORKER_COUNT):
w = Sorter(array, output, counts)
workers.append(w)
w.start()

_result = output.get()

total_count = 0
for _ in range(WORKER_COUNT):
total_count += counts.get()

for _ in range(WORKER_COUNT):
output.put("DEATH")

for w in workers:
w.join()

end_time = time.time()
trials["time"].append(end_time - start_time)
trials["cycles"].append(total_count)

return trials

def plot_chart(list_lengths, results) -> None:
# Init chart
fig, axarr = plt.subplots(2, 1, figsize=(8, 6))

# Average time graph
# Plot runtime to the graph
for i, (length, trial) in enumerate(zip(list_lengths, results)):
trial_time = np.ones(TRIAL_COUNT) * length
axarr.plot(trial_time, np.log(trial["time"]), "rx", alpha=0.4)

# Plot average result
avg_result = [np.log(sum(t["time"]) / len(t["time"])) for t in results]
axarr.plot(list_lengths, avg_result, label="Average Result")

# Chart labels
axarr.legend(loc=0)
axarr.set_xlabel("Length of Initial List")
axarr.set_ylabel("Average Time Elapsed - ln(seconds)")

# Average operation graph
# Plot cycles to graph
for i, (length, trial) in enumerate(zip(list_lengths, results)):
trial_ops = np.ones(TRIAL_COUNT) * length
axarr.plot(trial_ops, np.log(trial["cycles"]), "rx", alpha=0.4)

# Plot average result
avg_result = [np.log(sum(t["cycles"]) / len(t["cycles"])) for t in results]
axarr.plot(list_lengths, avg_result, label="Average Result")

# Plot n . n!
n_dot = np.log([n * factorial(n) for n in list_lengths])
axarr.plot(list_lengths, n_dot, label=r"$n \cdot n!$",)

# chart labels
axarr.legend(loc=0)
axarr.set_xlabel("Length of Initial List")
axarr.set_ylabel("Average Time Elapsed - ln(Operations)")

fig.suptitle("Parallel Bogosort")
plt.tight_layout()

# Save the plot
plt.savefig("bogosort.png")

def main():
list_lengths = range(2, 10)  # Random length for the unsorted lists
trial_results = []

# Run trials and add results to array
for list_len in list_lengths:
trial_info = run_trial(list_len)
trial_results.append(trial_info)

# Plot info to chart and save as PNG
plot_chart(list_lengths, trial_results)

if __name__ == "__main__":
sys.exit(main())