-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSleepSorter.java
86 lines (73 loc) · 1.67 KB
/
SleepSorter.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
78
79
80
81
82
83
84
85
86
public class SleepSorter {
private int sortingTime;
private boolean sortingInProgress=false;
public SleepSorter(int time){
if(time<=0)throw new IllegalArgumentException("time has to be >0");
sortingTime=time;
}
public void setTime(int time){
if(time<=0)throw new IllegalArgumentException("time has to be >0");
if(!sortingInProgress)sortingTime=time;
}
private int[]b;
int j;
public void sort(int[]a){
if(sortingInProgress)throw new RuntimeException("one SleepSorter can only sort one array at a time");
b=a;
j=0;
sortingInProgress=true;
int min=min(a);
Thread[]c=new Thread[a.length];
for(int i=0;i<a.length;i++){
int pom=a[i];
c[i]=new Thread(){
int val=pom;
int minCor=min;
@Override
public void run(){
try {
Thread.sleep((val-minCor)*sortingTime);
sorted(val);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}
for(Thread t:c)t.start();
try {
Thread.sleep((max(a)-min+1)*sortingTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
while(sortingInProgress){
try {
Thread.sleep(sortingTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private synchronized void sorted(int n){
b[j++]=n;
if(j==b.length)sortingInProgress=false;
}
private static int min(int[]a){
int min=a[0];
for(int i=1;i<a.length;i++){
if(min>a[i]){
min=a[i];
}
}
return min;
}
private static int max(int[]a){
int max=a[0];
for(int i=1;i<a.length;i++){
if(max<a[i]){
max=a[i];
}
}
return max;
}
}