-
Notifications
You must be signed in to change notification settings - Fork 497
/
SleepSort.java
57 lines (51 loc) · 1.66 KB
/
SleepSort.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
import java.util.Vector;
/**
* A Java implementation of sleep sort.
* Time Complexity : O(max(input))
* (Will work for integers and not real numbers)
*/
public class SleepSort {
private static final int DELAY_CONST = 25;
public static void main(String[] args) {
int[] data = new int[] { 9, 2, 3, 4, 0 };
sleepSort(data);
for (int element : data) {
System.out.println(element);
}
}
private static void sleepSort(int[] data) {
if (data.length == 0 || data.length == 0)
return;
SorterThread[] sorterThreads = new SorterThread[data.length];
Vector<Integer> sortedList = new Vector<>();
for (int i = 0; i < sorterThreads.length; i++) {
sorterThreads[i] = new SorterThread(data[i], sortedList);
sorterThreads[i].start();
}
while (sortedList.size() != data.length) {
try {
Thread.sleep(DELAY_CONST * DELAY_CONST);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < data.length; i++) {
data[i] = sortedList.get(i);
}
}
private static class SorterThread extends Thread {
private int sleepTimer;
private Vector<Integer> target;
public SorterThread(int sleepTimer, Vector<Integer> target) {
this.sleepTimer = sleepTimer * DELAY_CONST;
this.target = target;
}
public void run() {
try {
Thread.sleep(sleepTimer);
} catch (Exception e) {
}
target.add(sleepTimer / DELAY_CONST);
}
}
}