Skip to content

Instantly share code, notes, and snippets.

@wowselim
Created September 15, 2015 09:46
Show Gist options
  • Save wowselim/14ca8823a4235556d7ea to your computer and use it in GitHub Desktop.
Save wowselim/14ca8823a4235556d7ea to your computer and use it in GitHub Desktop.
Muchos effective sorting
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