Have you ever wanted to leave sorting up to complete chance? Well that's what Bogo sort does; randomizes an array, and checks if it has been randomly put in order.

Statistically speaking, there is a (practically zero) chance that in an alternate universe, bogo sort is being hailed as a mystical algorithm that always sorts an array in O(n) time, breaking proofs of lower bound sorting algorithms.

It is probably the same universe where all coins land on head, and it's been established as a fact of life that bogo sort is an efficient algorithm, and coins always land on head.

However, we live in this universe and bogo sort is purposefully inefficient to act as a thought experiment and an educational tool on sorting algorithms.

What is Bogo Sort?

Bogo Sort, also known as stupid sort, slow sort, monkey sort, and shotgun sort, is a sorting algorithm that randomly shuffles an array, then tests if the array has been sorted or not. If it is not sorted, the array is shuffled again and the process repeats.

/* pseudo-code */
function bogoSort( int[] arr ) {
    // While the array is not sorted..
    while (not isSorted( int[] arr ) ) {
        // .. Shuffle it.
        shuffle( arr );
    }
}
// Auxiliary function to check if the array is sorted.
function isSorted( int[] arr ) {
    for ( int i = 0; i < arr.length - 1; i++ ) {
        if ( arr[i] > arr[i + 1] ) {
            return false;
        }
    }
    return true;
}

Analyzing Bogo Sort

Best Worst Memory
O(n) O(∞) O(1)

Because bogo sort has no criteria to improve from one iteration to the next, it could possibly continue ad infinitum, or it could even get it right the first try. Either case, it shouldn't be depended on to get the job done.

When to use Bogo Sort

  • When you are teaching other's about bogo sort.
  • When you are making a meme out of bogo sort.