蜗牛排序法,不比最快(史上最慢),蜗牛不比,public class
分享于 点击 4442 次 点评:70
蜗牛排序法,不比最快(史上最慢),蜗牛不比,public class
public class SortDemo { static CountDownLatch cdl = null; public static void main(String[] args) throws Exception { int[] sortList = {3,4,5,1,2,4,5,7}; long beforeSortTime = new Date().getTime(); cdl = new CountDownLatch(sortList.length); Timer t = new Timer(); for(int i : sortList){ t.schedule(new TimerTask() { private Object num; public TimerTask setNum(Object n){ num = n; return this; } public void run() { System.out.println(num); cdl.countDown(); } }.setNum(i), new Integer(i).hashCode()); } cdl.await(); long afterSortTime = new Date().getTime(); System.out.println(String.format("本次排序用时[%s]秒", "" + (afterSortTime-beforeSortTime))); t.cancel(); }//旧的import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Stack;public class SortDemo { static Stack<Integer> stack = new Stack<Integer>(); /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] sortList = {3,4,5,1,2,4,5,7}; long beforeSortTime = new Date().getTime(); for(int i : sortList){ stack.push(i); new Thread(){ public Thread setNum(int n){ num = n; return this; } private int num; public void run() { try { this.sleep(num*1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } stack.pop(); System.out.println(num); } }.setNum(i).start(); } while(!stack.empty()){ //System.out.println(stack.size()); } long afterSortTime = new Date().getTime(); System.out.println(String.format("本次排序用时[%s]秒", "" + (afterSortTime-beforeSortTime))); }}//该片段来自于http://byrx.net
用户点评