Bogobogosort

Introduction

Algorithm Description

Bogosort can be described as follows:

Check if your list of number is sorted. If so, stop. If not, go to step 2. Rearrange the list of numbers randomly. Go to step 1.

This is far too efficient.

Bogobogosort specifies how one should check if the list of numbers is sorted. It does it recursively, because as anyone who knows anything at all about computer science knows, recursion is always good and cool. To check if the list is sorted, use the following procedure:

Make a copy of the list of numbers. Sort the first n-1 elements of the copy using bogobogosort. Check to see if the nth element of the sorted copy is greater than the highest element of the first n-1 elements. If so, the copy is now sorted, else randomise the order of the elements of the copy and go to step 2. Check to see if the copy is in the same order as the original list.

Analysis

Bogobogosort was implemented in C++ and timing runs made for sorting n numbers, averaged over 20 trials:

n Time (seconds) 1 0.0000 2 0.0000 3 0.0008 4 0.0054 5 0.0745 6 450 7 run overnight, did not finish

Mike Rosulek writes:

I believe bogobogosort is O(n * (n!)n). Let BBS(n) be the expected time of bogobogosort on input size n, and SC(n) the expected time of the sort checking algorithm on input size n. Then BBS(n) = SC(n) + (1 - 1/n!) BBS(n)

// with probability 1/n!, we finish, otherwise repeat BBS(n)



SC(n) = O(n) + BBS(n-1) + (1-1/n)( BBS(n-1) + O(n) )

// with probability 1/n, the n-th guy is the largest, otherwise must shuffle and re-sort the first n-1 guys Then substituting in for SC (skipping some simplifications - the (1-1/n)*O(n) factor gets swallowed by the first O(n)): BBS(n) = O(n) + BBS(n-1) + (1-1/n) BBS(n-1) + (1-1/n!) BBS(n)

// multiply by n!

n! * BBS(n) = O(n*n!) + n! ( BBS(n) + 2 BBS(n-1) ) - BBS(n) - (n-1)!*BBS(n-1)

// rearrange

BBS(n) = O(n*n!) + (2n! - (n-1)!)BBS(n-1)

= O(n*n!) + O(n!) * BBS(n-1)

= O(n*n!) + O(n*(n!)2) + ... + O(n*(n!)n)

= O(n*(n!)n)