睡眠排序法,了解一下

今天看到一个奇葩排序算法,睡眠排序法。

 1 public class ArraySort implements Runnable{
 2     private String num;
 3     public ArraySort(int num){
 4         this.num = num +"";
 5     }
 6 
 7     public static void main(String[] args){
 8         //把这个数组升序输出
 9         int[] nums = {11,3,998,5455,1,152,990};
10         for(int i = 0;i < nums.length;i++){
11             new Thread(new ArraySort(nums[i])).start();
12         }
13     }
14 
15     public void run(){
16         try{
17             Thread.sleep(Integer.parseInt(num));
18             System.out.println(num);
19         }catch(Exception e){
20             e.printStackTrace();
21         }
22     }
23 }

是不是感觉很神奇呢,但是这个算法还是有行不通的地方,如负数;数值相近的时候,可能会有误差;输入数据很多时,这些线程不能看作是同时启动。

猜你喜欢

转载自www.cnblogs.com/gelh/p/9937069.html