Skip to content

Instantly share code, notes, and snippets.

@deanveloper
Created January 20, 2016 17:29
Show Gist options
  • Save deanveloper/f1002ac6665797ab1a19 to your computer and use it in GitHub Desktop.
Save deanveloper/f1002ac6665797ab1a19 to your computer and use it in GitHub Desktop.
Check efficiency of a bogosort
package me.dean;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainClass {
private static List<Byte> deck = new ArrayList<>();
public static void main(String[] args) {
for (byte i = 0; i < 14; i++) deck.add(i);
long start = System.currentTimeMillis();
int tries = 0;
do {
Collections.shuffle(deck);
tries++;
} while (deckShuffled());
System.out.println("Done shuffling!");
System.out.println("Time: " + (System.currentTimeMillis() - start) + " ms");
System.out.println("Tries: " + tries);
System.out.println("Should take " + factorial(deck.size()) + " tries");
}
private static boolean deckShuffled() {
byte first = -1;
for (byte next : deck) {
if(first == -1) {
first = next;
continue;
}
if(first > next) return true;
first = next;
}
return false;
}
private static long factorial(long value) {
for(long factor = value-1; factor > 1; factor--) {
value *= factor;
}
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment