睡觉排序
咕咕咕 fishing

前言

在网上看到了这个算法,觉得很是厉害。
能想出这种算法的多半是个人才,所以记录下,也算是分享。
写程序,要拓宽思路。

代码

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
import java.util.Scanner;

public class SleepSort implements Runnable {
private final int num;

public SleepSort(int num) {
this.num = num;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] nums = new int[10];
for (int i = 0; i < 10; i++) {
nums[i] = scanner.nextInt();
}
//排序
for (int j : nums) {
new Thread(new SleepSort(j)).start();
}
}

@Override
public void run() {
try {
Thread.sleep(num * 100);//乘100防止num值过小出错,不过nums中值相近时,还是容易出错。
System.out.println(num);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

后记

叹为观止的算法! 时间复杂度为O(max(input))