Bogosort

In computer science, bogosort[1][2] (also known as permutation sort, stupid sort,[3] slowsort,[4] shotgun sort, random sort, monkey sort, bobosort or shuffle 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.

Bogosort
ClassSorting
Data structureArray
Worst-case performanceUnbounded (randomized version), O((n+1)!) (expected runtime, randomized version)[1] O((n+1)!) (deterministic version)
Best-case performanceO(n)[1]
Average performanceO((n+1)!)[1]
Worst-case space complexityO(1)

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. Its name is a portmanteau of the words bogus and sort.[5]

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."""
    return all(data[i] <= data[i+1] for i in range(len(data)-1))

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";
 load "Int";
 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

Experimental runtime of bogosort

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 .[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 , 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.

Gorosort
is 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
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 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
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.[1] O(n!) is found to be the expected average case.
Worstsort
is a pessimal[lower-alpha 1] 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 algorithm is based on a bad sorting algorithm, . The badsort algorithm accepts two parameters: , which is the list to be sorted, and , which is a recursion depth. At recursion level , merely uses a common sorting algorithm, such as bubblesort, to sort its inputs and return the sorted list. That is to say, . Therefore, badsort's time complexity is if . However, for any , first generates , the list of all permutations of . Then, calculates , and returns the first element of the sorted . To make truly pessimal, may be assigned to the value of a computable increasing function such as (e.g. , where is Ackermann's function). Ergo, to sort a list arbitrarily badly, you would execute , where = number of elements in . The resulting algorithm has complexity , where = factorial of iterated times. This algorithm can be made as inefficient as we wish by picking a fast enough growing function .[8]
Slowsort
A different humorous sorting algorithm that employs a misguided divide-and-conquer strategy to achieve massive complexity.
gollark: OH NO, the issue SPREAD to the nonwebsocket mode?!
gollark: I have unfortunately disabled the websocket status link as it just *does not work* consistently.
gollark: Oh no, it recascaded.
gollark: Uncascaded, but the now playing thing is just *filled* with apioforms.
gollark: Okay, osmarks internet radio™ appears to be undergoing cascading failure.

See also

References

  1. Gruber, H.; Holzer, M.; Ruepp, O., "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, 4475, Springer-Verlag, pp. 183–197, doi:10.1007/978-3-540-72914-3_17.
  2. 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, archived from the original (PDF) on 26 March 2012, retrieved 22 June 2011
  3. E. S. Raymond. "bogo-sort". The New Hacker’s Dictionary. MIT Press, 1996.
  4. Naish, Lee (1986), "Negation and quantifiers in NU-Prolog", Proceedings of the Third International Conference on Logic Programming, Lecture Notes in Computer Science, 225, Springer-Verlag, pp. 624–634, doi:10.1007/3-540-16492-8_111.
  5. "Bogosort". The Jargon File 4.4.8. 2003. Retrieved 11 April 2013.
  6. Google Code Jam 2011, Qualification Rounds, Problem D
  7. Bogobogosort
  8. Lerma, Miguel A. (2014). "How inefficient can a sort algorithm be?". arXiv:1406.1077 [cs.DS].
  1. The opposite of "optimal"

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.