代码如下
public class SellpSort {
public static void main(String[] args){
int[] ints = {1,3,5,6,8,22,11,0,99};
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);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println(ms);
}
}
执行效果

算法原理
它原理是构造n个线程,它们和这n个数一一对应。
初始化后,线程们开始睡眠,等到对应的数那么多个时间单位后各自醒来,然后输出它对应的数。
这样最小的数对应的线程最早醒来,这个数最早被输出。
等所有线程都醒来,排序就结束了。
本文介绍了一种名为卖牌排序的创新排序算法。该算法通过创建与待排序数组元素数量相等的线程,每个线程根据其对应的数值进行相应时间长度的休眠,从而实现排序。最小的数对应的线程将首先完成休眠并输出其数值,最终得到一个有序数组。此方法打破了传统排序算法的时间复杂度概念。
533

被折叠的 条评论
为什么被折叠?



