-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathCode.java
76 lines (62 loc) · 1.92 KB
/
Code.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// import libraries {
import org.algorithm_visualizer.*;
import java.util.*;
// }
class Main {
// define tracer variables {
Array1DTracer arrayTracer = new Array1DTracer("Array");
LogTracer logTracer = new LogTracer("Console");
// }
boolean isSorted(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1]) {
return false;
}
}
return true;
}
int[] shuffle(int[] array) {
for (int i = 0; i < array.length; i++) {
int randomIndex = new Randomize.Integer(0, array.length - 1).create();
int temp = array[i];
array[i] = array[randomIndex];
array[randomIndex] = temp;
}
return array;
}
void bogosort(int[] array) {
while (!isSorted(array)) {
logTracer.println("Array not sorted: " + Arrays.toString(array));
// visualize {
arrayTracer.set(array);
Tracer.delay();
// }
logTracer.println("Shuffling array...");
array = shuffle(array);
}
logTracer.println("Array sorted: " + Arrays.toString(array));
// visualize {
arrayTracer.set(array);
for (int i = 0; i < array.length; i++) {
arrayTracer.select(i);
Tracer.delay();
}
// }
}
Integer[] uniqueArray = (Integer[]) new Randomize.Array1D(4, new Randomize.Integer(1, 9)).create();
Main() {
int a = uniqueArray[0];
int b = uniqueArray[1];
int c = uniqueArray[2];
int d = uniqueArray[3];
// visualize {
Layout.setRoot(new VerticalLayout(new Commander[]{arrayTracer, logTracer}));
arrayTracer.set(new int[]{a, b, c, d});
Tracer.delay();
// }
bogosort(new int[]{a, b, c, d});
}
public static void main(String[] args) {
new Main();
}
}