Created
September 15, 2015 09:46
-
-
Save wowselim/14ca8823a4235556d7ea to your computer and use it in GitHub Desktop.
Muchos effective sorting
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
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
public class Bogosort { | |
public static void main(String[] args) { | |
List<Integer> numbers = Arrays.asList(5, 2, 6, 2, 6, 8, 32, 7, 234, | |
123, 5, 23, 6324, 7, 123, 7234, 723452345, 234246); | |
int iterations = 0; | |
while (!isSorted(numbers)) { | |
Collections.shuffle(numbers); | |
iterations++; | |
} | |
System.out.printf("Took %d iterations.", iterations); | |
} | |
private static <T extends Comparable<? super T>> boolean isSorted(List<T> l) { | |
List<T> clone = new ArrayList<>(l); | |
Collections.sort(l); | |
return l.equals(clone); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment