java排序算法之睡眠排序

  |   2,920 浏览

    说到java排序算法,我们大脑第一反应就是:快速排序、冒泡排序等等。

    下面给大家介绍一款比较有意思的排序算法(仅供娱乐,千万别用于生产)
    传说中的睡眠大法:

    public class Sort{
    
        public static void main(String[] args) {
            int[] nums = new int[]{1, 3, 10, 8, 20, 2, 90};
            for(int i=0; i<nums.length; i++){
                sortLikeGod(nums[i]);
            }
        }
    
        private static void sortLikeGod(final int num) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(num);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(num);
                }
            }).start();
        }
    }
    
    

    运行main方法后,控制台会假装排好序似的打印如下图:

    imagepng