forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBogoSort.java
More file actions
47 lines (39 loc) · 968 Bytes
/
Copy pathBogoSort.java
File metadata and controls
47 lines (39 loc) · 968 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
39
40
41
42
43
44
45
46
47
/*
an implementation of bogosort
input from command line
to increasing order
*/
import java.util.Random;
public class BogoSort {
public static void main(String[] args) {
int[] arr = new int[args.length];
for (int i = 0; i < args.length; i++)
arr[i] = Integer.parseInt(args[i]);
while(!isSorted(arr)) arr = scramble(arr);
disp(arr);
}
private static boolean isSorted(int[] arr) {
for (int i = 0; i < arr.length - 1; i++)
if (arr[i + 1] < arr[i])
return false;
return true;
}
private static int[] scramble(int[] arr) {
Random rand = new Random();
int[] scr = new int[arr.length], chk = new int[arr.length];
int count = 0, index;
while (count < arr.length) {
index = rand.nextInt(arr.length);
if (chk[index] != 1) {
scr[index] = arr[count];
chk[index] = 1;
count++;
}
}
return scr;
}
private static void disp(int[] arr) {
for(int i : arr) System.out.print(i + " ");
System.out.println();
}
}