Created
January 20, 2016 17:29
-
-
Save deanveloper/f1002ac6665797ab1a19 to your computer and use it in GitHub Desktop.
Check efficiency of a bogosort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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