Java bogosort
import java.math.*;
import java.util.Arrays;
public class Test {
public static void shuffle(int[] arr) {
for(int i = 0; i < arr.length; i++) {
int index = (int) (Math.random() * arr.length);
int temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
public static boolean isSorted(int[] arr) {
for(int i = 1; i < arr.length; i++) {
if(arr[i] < arr[i-1]) {
return false;
}
}
return true;
}
public static void bogoSort(int[] arr) {
while(!isSorted(arr)) {
shuffle(arr);
}
}
public static void main(String[] args) {
int[] arr = {3, 31, 16, 8};
bogoSort(arr);
System.out.println(Arrays.toString(arr));
}
}
Jacob Burns