-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSort.java
68 lines (59 loc) · 1.7 KB
/
LSort.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
package LSort;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
public class LSort {
private final int ub = 1000;
private int r, mod;
private final int[] arr;
public LSort(int[] source, boolean insertionSort, int mod) {
final int N = source.length;
r = (int) Math.sqrt(N);
r = r > ub ? r : ub;
if (mod == 0) {
this.mod = N / ub + 1;
} else {
this.mod = mod;
}
this.mod = (int) Math.ceil(Math.log(this.mod));
arr = source;
if (insertionSort || N < ub) {
InsertionSort.sort(arr, 0, arr.length);
} else {
ForkJoinPool.commonPool().invoke(new MSDSort(arr, 0, arr.length, getMSD(0, arr.length), this.mod, r));
}
}
private int getMSD(int begin, int end) {
if (mod < 2) {
return 0;
}
int result = -1;
for (int i = begin; i < end; ++i) {
int count = 0, t = arr[i];
while (t >> mod != 0) {
t >>= mod;
++count;
}
if (count > result) {
result = count;
}
}
return result;
}
public int[] getResult() {
return arr;
}
public static void main(String[] args) {
Random random = new Random();
int[] a = new int[1000];
for (int i = 0; i < 1000; ++i) {
a[i] = random.nextInt(10000000);
System.out.printf("%d ", a[i]);
}
System.out.println();
LSort lsort = new LSort(a, false, 0);
for (int i = 0; i < 1000; ++i) {
System.out.printf("%d ", a[i]);
}
System.out.println();
}
}