别的地方看过,感觉挺好玩,转之
public class SleepSort {
public static void main(String[] args) {
int[] ints = {1,4,7,3,8,9,2,6,5};
SortThread[] sortThreads = new SortThread[ints.length];
for (int i = 0; i < sortThreads.length; i++) {
sortThreads[i] = new SortThread(ints[i]);
}
for (int i = 0; i < sortThreads.length; i++) {
sortThreads[i].start();
}
}
}
class SortThread extends Thread{
int ms = 0;
public SortThread(int ms){
this.ms = ms;
}
public void run(){
try {
sleep(ms*10+10);//利用Thread的sleep时间来让大的数,sleep停留时间长来过滤,从而实现排序!BT
} catch (InterruptedException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(ms);
}
}
还有JavaScript版的,比较短:
function sleepsort() {
for (var i = 0, il = arguments.length; i < il; i++) {
(function(args, index) {
setTimeout(function() {
document.body.innerHTML += args[index] + ', ';
}, args[index]);
}(arguments, i));
}
};
不解释,BH的程序不需要理由……
史上BT的排序算法
最新推荐文章于 2023-11-07 14:02:31 发布