编程珠玑(1)

开始看了下编程珠玑,书的开篇就是一个排序的问题,今天就来实践一下,稍作修改,如题目大意:生成1千万个1亿以内的不重复的数据集合.

public final static int count = 100000000;
private static boolean[] collect = new boolean[count];
private static int[] source = new int[count/10];

public void createDate() {
	int temp  = 0;
	for(int i = 0; i < count/10;) {
                temp = new Random().nextInt(count);
		if(!collect[temp]) {
			source[i] = temp;
			collect[temp] = true;
			System.out.println(i + " = " + source[i] );
			i++;
		}
	}
}

 这通过boolean来标识的话,速度确实加快了不少,直接变为了近O(n)了,不知道还能不能进行改进~~~

 

你可能感兴趣的:(java,Algorithm)