[代碼] [Java]代碼 import java.util.Random; public class FindTop100_5 { /** * @param args */ public static void main(String[] args) throws Exception { final int N=100; int[] max = new int[N]; http://www.fpnanchang.com/linked/20130222.do; int index = 0; Random r = new Random(); long start = System.currentTimeMillis();//計時開端 while (index <100000000) { int temp = r.nextInt(); //System.out.print(temp " "); // 前N個,選用疾速查找的方法找到最適宜方位,刺進新數據 並確保有序 // 排序方法是從大到小 int low = 0; int high = Math.min(index, max.length - 1); int mid = 0; while (low <= high) { mid = (low high) / 2; if (max[mid] < temp) high = mid - 1; else low = mid 1; } //判定新數的方位後移動數據,騰出空間放置新數 //固然,可以新數在當時的數據下不能進入(比最小的還小),j>low就不會創建,數據不會移動 for (int j = Math.min(index, max.length - 1); j > low; j--) { max[j] = max[j - 1]; } if (low < max.length)//只需找到滿意條件的個數的便可,避免越界 max[low] = temp;// 放置到適宜方位 index ; } long time = System.currentTimeMillis() - start; System.out.println("\n最大的" max.length "個是:"); for (int t : max) { System.out.print(t "\t"); } System.out.println("\n耗時:" time "毫秒"); } } http://www.fpfuzhou.com/linked/20130222.do;