Java~大数据下的TopK(相对于普通PriorityQueue解决TopK优化其时间效率和空间效率)

TopK问题
给定一个集合(元素个数很多N),想找到最大的或者最小的k个元素。

以找k个最大为例的俩种TopK方法:

  1. 之前讲过一次利用库函数里的普通优先级队列完成。
    https://blog.csdn.net/Shangxingya/article/details/105879723
  2. 建立大小为k的小堆,堆顶元素一定是这k个里最小的,然后循环遍历剩下N-k个元素,分别和当前堆顶元素进行比较,如果比此时堆顶元素打,就直接替换,并且向下进行堆调整,得到新的堆顶元素,当所有元素遍历完,堆中就剩下前k个最大值。

或许有人疑问第一个简便为什么要使用第二个。

  • 为了空间效率
    当我们的N>>k的时候,也就是说比如我们有100亿个数据,即使是100亿个int型数据,我们用第一种方法大约需要40G的内存

  • 为了时间效率
    如果有100亿个数据,我们进行向上向下调整的时候比较和互换值的时间浪费太多。

实现:

  • 在构造方法中设置我们的k
//构造k个位置
    public PriorityQueueForTopK(int k) {
        this.array = new int[k];
    }
  • 入队列,如果k个位置未满直接入队列然后进行向上调整,如果满了就与队首的元素进行比较然后进行向下调整。
//入队列
    public void offer(int data) {
        if(size < array.length) {
            array[size] = data;
            smallShiftUp(size);
            size ++;
        }else {
            compareOfferFirst(data);
        }
    }
    //与队首进行比较入队列
    private void compareOfferFirst(int data) {
        if(data > this.array[0]) {
            this.array[0] = data;
            smallShiftDown(0);
        }
    }
  • 剩下的出队列,取队首元素,判空操作不变。

实现:

public class PriorityQueueForTopK {
    private int[] array;
    private int size;
    //构造k个位置
    public PriorityQueueForTopK(int k) {
        this.array = new int[k];
    }
    //入队列
    public void offer(int data) {
        if(size < array.length) {
            array[size] = data;
            smallShiftUp(size);
            size ++;
        }else {
            compareOfferFirst(data);
        }
    }
    //向上调整建小堆
    private void smallShiftUp(int index) {
        int child = index;
        int parent = (child - 1) / 2;
        while (child > 0) {
            if(this.array[parent] > this.array[child]) {
                int tmp = this.array[child];
                this.array[child] = this.array[parent];
                this.array[parent] = tmp;
            }else {
                break;
            }
            child = parent;
            parent = (child - 1) / 2;
        }
    }
    //与队首进行比较入队列
    private void compareOfferFirst(int data) {
        if(data > this.array[0]) {
            this.array[0] = data;
            smallShiftDown(0);
        }
    }
    //向下调整建小堆
    private void smallShiftDown(int index) {
        int parent = index;
        int child = 2 * parent + 1;
        while (child < size) {
            if(child + 1 < size && array[child] > array[child + 1]) {
                child ++;
            }
            if(array[child] < array[parent]) {
                int tmp = this.array[child];
                this.array[child] = this.array[parent];
                this.array[parent] = tmp;
            }else {
                break;
            }
            parent = child;
            child = 2 * parent + 1;
        }
    }
    //出队列
    public Integer poll() {
        if(size == 0) {
            return null;
        }
        int ret = this.array[0];
        System.arraycopy(this.array, 1, this.array, 0, array.length - 1);
        size --;
        return ret;
    }
    //查看队首元素
    public Integer peek() {
        if(this.size == 0) {
            return null;
        }
        return this.array[0];
    }
    //判空
    public boolean empty() {
        return this.size == 0;
    }

}

测试:

public class Test {
    public static void main(String[] args) {
        PriorityQueueForTopK topk = new PriorityQueueForTopK(3);
        int[] nums = new int[] {11,22,33,12,45,101,76,89,65,1,2,3,4,5,6,102,7,8,9,0,99,13,88,21,17,100};
        for (int i : nums
             ) {
            topk.offer(i);
        }
        System.out.println(topk.peek());
        while (!topk.empty()) {
            System.out.print(topk.poll() + " ");
        }
    }
}

测试结果:

100
100 102 101 
Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/Shangxingya/article/details/105904054
今日推荐