Bogo-sort kan bedst beskrives som en "archetypical perversely awful algorithm". Den svarer til "repeatedly throwing a deck of cards in the air, picking them up at random, and then testing whether they are in order", The Jargon File. Det negative syn på bogo-sort skyldes sandsynligvis, at den er ubrugelig sløv, så som i variaten herunder, men dette skal ikke hindre en i at holde af den – og til tider har den ligefrem en form for skønhed over sig.
import java.util.Random;
public abstract class BogoSort {
private static Random ranGen = new Random();
private static Comparable[] theArray;
public static void bogoSort(Comparable[] _theArray){
theArray = _theArray;
while(!isSorted()) { permutate(); }
}
private static boolean isSorted(){
for(int i = 0; i < theArray.length-1; i++)
if(theArray[i].compareTo(theArray[i+1]) > 0)
return false;
return true;
}
private static void permutate(){
for(int i = 0; i < theArray.length; i++){
int no = ranGen.nextInt(theArray.length-i)+i;
Comparable x = theArray[i];
theArray[i] = theArray[no];
theArray[no] = x;
}
}
}