- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathBogoSortIn_java.java
More file actions
Latest commit
54 lines (45 loc) · 1.4 KB
/
Copy pathBogoSortIn_java.java
File metadata and controls
54 lines (45 loc) · 1.4 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
importjava.util.Random;
publicclassBogoSortIn_java {
privatestaticfinalintSIZE = 10;
publicstaticvoidshuffle(int[] array) {
for (inti = 0; i < array.length; i++) {
intj = (int) (Math.random() * array.length);
inttemp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
publicstaticbooleanisSorted(int[] array) {
for (inti = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
returnfalse;
}
}
returntrue;
}
publicstaticvoidbogosort(int[] array) {
intshuffles = 0;
while (!isSorted(array)) {
shuffle(array);
shuffles++;
}
// System.out.println("Number of shuffles: " + shuffles);
}
publicstaticvoidmain(String[] args) {
int[] array = newint[SIZE];
for (inti = 0; i < array.length; i++) {
array[i] = (int) (Math.random() * 100);
}
System.out.print("The unsorted array is:\t");
for (inti = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
bogosort(array);
System.out.print("The sorted array is:\t");
for (inti = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}