此排序法为从知乎看,定义为睡眠排序法,有误差性实用性不大
整数数组排序
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 从小数来看,如果小数相近的话,不一定会谁先输出
总体来说不知道谁的哪位大佬的思维这么活跃想到用睡眠来排序,娱乐就好