位图排序:从1亿个数字中取出最大的100个

package com.zetyun.test;

import java.util.Random;

public class Top100 {
public static int[] getTop100(int[] inputArray) {

int maxValue = Integer.MIN_VALUE;
for (int i = 0; i < inputArray.length; i++) {
if (maxValue < inputArray[i]) {
maxValue = inputArray[i];
}
}
byte[] bitmap = new byte[maxValue+1];
for (int i = 0; i < inputArray.length; i++) {
int value=inputArray[i];
bitmap[value] = 1;
}

int[] result = new int[100];
int index = 0;
for (int i = maxValue; i >= 0 & index < 100; i--) {
if (bitmap[i] == 1) {
result[index++] = i;
}
}
return result;
}

public static void main(String[] args) {
int numberCount = 100000000;
int maxNumber = numberCount;
int inputArray[] = new int[numberCount];
Random random = new Random();
for (int i = 0; i < numberCount; i++) {
inputArray[i] = Math.abs(random.nextInt(maxNumber));
}
System.out.println("Sort begin...");
long current = System.currentTimeMillis();
int[] result = Top100.getTop100(inputArray);
System.out.println(System.currentTimeMillis() - current);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + ",");
}
}
}
posted @ 2018-04-24 14:56  java与大数据征程  阅读(1510)  评论(0编辑  收藏  举报