Name Best-case Average-case Worst-case Memory Stable
BogoSort n (n × n!) 1 no

BogoSort


Your browser does not support the HTML5 canvas tag.

Java Code

public void bogoSort(int[] array) {
     while (!isSorted(array)) {
         shuffle (array);
     }
}

public boolean isSorted(int[] array) {
     for (int i = 0; i < array.length - 1; i++) {
         if (array[ i ] > array[ i + 1 ])
             return false;
     }
     return true;
}

public void shuffle(int[] array) {
     for (int i = array.length - 1; i > 0; i--) {
         swapKeys ( array , i , (int) (Math.random() * (i + 1)) );
     }
}

public void swapKeys(int[] array, int i, int j) {
     int temp;
     temp = array[ i ];
     array[ i ] = array[ j ];
     array[ j ] = temp;
}
            

How to use

Use the textfield to type in a number and add it by either pressing ENTER or by clicking on the "Add" button.
You can also add 1 random number by clicking on the "1 Random Key" button. Overall you can add up to 5 keys.
The "Sort" button starts to sort the keys with the selected algorithm. Alternatively you can sort 8 random keys fast for a quick impression of how the algorithm works.