十億數據中求最大一百個數字

Stackoverflow看到的問題,真是討論的熱火朝天。

前期看過相關大數據處理的一些文章,有新的處理方式,於是藉此機會實現了一份代碼。

但是Stackoverflow的Reputation過低,還無法回答問題。在此先保存下來,後期再回復。

package org.example.bigdata;

public class TopNumber {
	public static void main(String[] args) {
		final int input[] = {2389,8922,3382,6982,5231,8934
							,4322,7922,6892,5224,4829,3829
							,6892,6872,4682,6723,8923,3492};
		//One int(4 bytes) hold 32 = 2^5 value,
		//About 4 * 125M Bytes
		//int sort[] = new int[1 << (32 - 5)];
		//Allocate small array for local test
		int sort[] = new int[1000];
		//Set all bit to 0
		for(int index = 0; index < sort.length; index++){
			sort[index] &= 1 ^ 1;
		}
		for(int number : input){
			sort[number >>> 5] |= (1 << (number % 32));
		}
		int topNum = 0;
		outer:
		for(int index = sort.length - 1; index >= 0; index--){
			if((1 ^ 1) != sort[index]){
				for(int bit = 31; bit >= 0; bit--){
					if(0 != (sort[index] & (1 << bit))){
						System.out.println((index << 5) + bit);
						topNum++;
						if(topNum >= 3){
							break outer;
						}
					}
				}
			}
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章