-
Notifications
You must be signed in to change notification settings - Fork 108
/
BogoSort.java
77 lines (66 loc) · 2.08 KB
/
BogoSort.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
77
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
/**
* This class holds an example for a Bogo-Sort implementation with a main as an example.
*
* @author Paul2708
*/
public final class BogoSort {
/**
* Run the sorting algorithm with a sample array.
*
* @param args ignored
*/
public static void main(String[] args) {
int[] array = {5, 3, 0, 8, 2, 4};
System.out.println(Arrays.toString(array));
sort(array);
System.out.println(Arrays.toString(array));
}
/**
* Sort an array based on Bogo-Sort.
* <p>
* Bogo-Sort is one of the dumbest ways to sort an array.
* It is full random and shouldn't be used at all!
* It's so inefficient that there isn't any lower bounds in O-notation.
* (It's just random, right?)
* <p>
* The algorithm swaps two indices in the array... until the array is sorted.
* <p>
* More information: https://en.wikipedia.org/wiki/Bogosort
*
* @param array unsorted array of integer
*/
public static void sort(int[] array) {
Random random = ThreadLocalRandom.current();
while (!isSorted(array)) {
// Create some random indices
int indexA = random.nextInt(array.length);
int indexB = random.nextInt(array.length);
// Swap index a with b
int temp = array[indexA];
array[indexA] = array[indexB];
array[indexB] = temp;
}
}
/**
* Check if an array is sorted.
* An array is sorted if it's empty or:
* <code>a[i] <= a[i + 1]</code> for every <code>i</code> in 0..a.length - 1
*
* @param array array
* @return true if the array is sorted, otherwise false
*/
private static boolean isSorted(int[] array) {
if (array.length == 0 || array.length == 1) {
return true;
}
for (int i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1]) {
return false;
}
}
return true;
}
}