-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBozoSort.java
More file actions
57 lines (44 loc) · 1.61 KB
/
BozoSort.java
File metadata and controls
57 lines (44 loc) · 1.61 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
55
56
57
package com.pslin.algorithms.sort;
import org.apache.commons.math3.util.CombinatoricsUtils;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Random;
/**
* While not sorted, swap two random numbers.
*
* @author plin
*/
public class BozoSort {
public static void main(String[] args) {
int length;
if(args.length == 0) {
length = 13;
} else {
length = Integer.parseInt(args[0]);
}
int[] numbers = ArrayUtils.createArray(length);
ArrayUtils.shuffle(numbers);
System.out.println(Arrays.toString(numbers));
long count = 0;
double start = System.currentTimeMillis();
while(!ArrayUtils.isSorted(numbers)) {
swapRandom(numbers);
count++;
}
System.out.println("\n" + Arrays.toString(numbers));
DecimalFormat decimalFormat = new DecimalFormat("#,###");
double time = System.currentTimeMillis() - start;
System.out.println("Time: " + time + " ms");
System.out.println(time / 1000 + " sec");
System.out.println("Number of shuffles: " + decimalFormat.format(count));
System.out.println("Expected average case: " + decimalFormat.format(CombinatoricsUtils.factorial(length)));
}
private static void swapRandom(int[] numbers) {
Random random = new Random();
int a = random.nextInt(numbers.length);
int b = random.nextInt(numbers.length);
int temp = numbers[a];
numbers[a] = numbers[b];
numbers[b] = temp;
}
}