-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBogoSort.java
More file actions
38 lines (29 loc) · 850 Bytes
/
BogoSort.java
File metadata and controls
38 lines (29 loc) · 850 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
32
33
34
35
36
37
38
import java.util.Random;
public class BogoSort {
public static void main(String[] args) {
int[] field = {23, 42, 137, 12, 54};
int[] result = bogo(field);
for (int element : result) {
System.out.print(element + " ");
}
}
public static int[] bogo(int[] field) {
Random r = new Random();
while (!isFinished(field)) {
int a = r.nextInt(field.length);
int b = r.nextInt(field.length);
int temp = field[a];
field[a] = field[b];
field[b] = temp;
}
return field;
}
public static boolean isFinished(int[] field) {
for (int i = 0; i < field.length - 1; i++) {
if (field[i] > field[i + 1]) {
return false;
}
}
return true;
}
}