睡眠排序法,当娱乐就好

此排序法为从知乎看,定义为睡眠排序法,有误差性实用性不大
整数数组排序

public class ArraySort implements Runnable{
    private String  num;
    public  ArraySort(int num){
        this.num=num+"";
    }

    /**
     * 睡眠排序法---谁先醒来谁当头,有误差的可能性
     * @param args
     */
    public static void main(String[] args) {
        int[] array={1,5,10,20,3,500,100,201};
        for(int i=0; i<array.length; i++){
            new Thread(new ArraySort(array[i])).start();
        }
    }
    public void run() {
        try {
            /**
             * 睡眠时间为自定义的参数---单位ms
             */
            Thread.sleep(Integer.parseInt(num));
            System.out.println(num);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

输出结果为:1,3,5,10,20,100,201,500 总体看来整数的输出结果还不错但是小数就出问题了

public class ArraySort implements Runnable{
    private double num;
    public  ArraySort(double num){
        this.num=num;
    }

    /**
     * 睡眠排序法---谁先醒来谁当头,有误差的可能性
     * @param args
     */
    public static void main(String[] args) {
        double[] array={10.1,10.2,10.3,10.4,10.5,10.6,10.7,10.8,11.0,12.0,9.0};
        for(int i=0; i<array.length; i++){
            new Thread(new ArraySort(array[i])).start();
        }
    }
    public void run() {
        try {
            /**
             * 睡眠时间为自定义的参数---单位ms
             */
            num= Double.parseDouble(num+"");
            Thread.sleep((long) num);
            System.out.println(num);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

输出结果为:10.3,10.1,9.0,10.7,10.5,10.6,10.2,10.4,11.0,10.8,12.0 从小数来看,如果小数相近的话,不一定会谁先输出
总体来说不知道谁的哪位大佬的思维这么活跃想到用睡眠来排序,娱乐就好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值