forked from kennyledet/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBogosort.java
More file actions
31 lines (27 loc) · 731 Bytes
/
Bogosort.java
File metadata and controls
31 lines (27 loc) · 731 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.Random;
public class Bogosort {
public static int[] sort(int[] arr) {
while (!isSorted(arr)) {
fisher_yates(arr);
}
return arr;
}
private static boolean isSorted(int[] arr) {
for (int i = 1; i < arr.length; i++) {
if (arr[i] < arr[i-1]) return false;
}
return true;
}
// Soley used for shuffling, not part of actual algorithm
private static void fisher_yates(int[] arr) {
Random rand = new Random();
int m = arr.length;
int tmp, i;
while (m != 0) {
i = rand.nextInt(m--);
tmp = arr[m];
arr[m] = arr[i];
arr[i] = tmp;
}
}
}