한 무더기 의 난수 중에서 가장 큰 몇 개 를 찾아내다

[  ] [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("
" max.length " :"); for (int t : max) { System.out.print(t "\t"); } System.out.println("
:" time " "); } } http://www.fpfuzhou.com/linked/20130222.do;

좋은 웹페이지 즐겨찾기