在处理大量数据的时候,有时候每每须要找出Top前几的数据,这时候若是直接对数据进行排序,在处理海量数据的时候每每就是不可行的了,并且在排序最好的时间复杂度为nlogn,当n远大于须要获取到的数据的时候,时间复杂度就显得太高。
使用最小堆或者最大堆能够很好地解决Top大问题或者Top小问题。java
对于n个数,取Top m个数,时间复杂度为O(nlogm),这样在n较大状况下,是优于nlogn的时间复杂度的。数据结构
好比10000个数据,取前100大的数,那么时间复杂度就是O(10000log100)。
由于在插入数据的时候须要遍历元素时间复杂度达到了O(10000),而后每次插入过程当中进行调整的复杂度为O(log100),因此整体时间复杂度为O(10000log100)。ide
Java集合中的PriorityQueue就能够实现最大堆或者最小堆,从名字能够知道该集合是优先队列,数据结构中的优先队列就是使用堆来实现的。函数
// 底层经过一个Object类型数据保存元素 transient Object[] queue; // 经过Comparator制定比较方法 private final Comparator<? super E> comparator; // 其中一个构造函数 public PriorityQueue(int initialCapacity, Comparator<? super E> comparator) { // Note: This restriction of at least one is not actually needed, // but continues for 1.5 compatibility if (initialCapacity < 1) throw new IllegalArgumentException(); this.queue = new Object[initialCapacity]; this.comparator = comparator; }
下面就使用PriorityQueue来实现最小堆和最大堆。this
public class TopK<E extends Comparable> { private PriorityQueue<E> queue; private int maxSize; //堆的最大容量 public TopK(int maxSize) { if (maxSize <= 0) { throw new IllegalStateException(); } this.maxSize = maxSize; this.queue = new PriorityQueue<>(maxSize, new Comparator<E>() { @Override public int compare(E o1, E o2) { // 最大堆用o2 - o1,最小堆用o1 - o2 return (o1.compareTo(o2)); } }); } public void add(E e) { if (queue.size() < maxSize) { queue.add(e); } else { E peek = queue.peek(); if (e.compareTo(peek) > 0) { queue.poll(); queue.add(e); } } } public List<E> sortedList() { List<E> list = new ArrayList<>(queue); Collections.sort(list); return list; } public static void main(String[] args) { int[] array = {4, 5, 1, 6, 2, 7, 3, 8}; TopK pq = new TopK(4); for (int n : array) { pq.add(n); } System.out.println(pq.sortedList()); } }
运行结果:
rest
经过上述讲述,基本了解最大堆和最小堆状况以及它们与TopK问题的关系,上面是使用集合实现,下面使用Java来实现最小堆,并解决TopK大问题。code
public class TopK { int[] items; int currentSize = 0; // 初始化为size + 1,从下标1开始保存元素。 public TopK(int size) { items = new int[size + 1]; } // 插入元素 public void insert(int x) { if (currentSize == items.length - 1) { if (compare(x, items[1]) < 0) { return; } else if (compare(x, items[1]) > 0) { deleteMin(); } } int hole = ++currentSize; for (items[0] = x; compare(x, items[hole / 2]) < 0; hole /= 2) { items[hole] = items[hole / 2]; } items[hole] = x; } // 删除最小堆中最小元素 public int deleteMin() { int min = items[1]; items[1] = items[currentSize--]; percolateDown(1); return min; } // 下滤 public void percolateDown(int hole) { int child; int temp = items[1]; for (; hole * 2 <= currentSize; hole = child) { child = 2 * hole; if (child != currentSize && compare(items[child + 1], items[child]) == -1) { child++; } if (compare(items[child], temp) < 0) { items[hole] = items[child]; } else { break; } } items[hole] = temp; } // 制定比较规则 public static int compare(int a, int b) { if (a < b) { return -1; } else if (a > b) { return 1; } return 0; } public static void main(String[] args) { TopK topK = new TopK(10); for (int i = 1; i <= 100; i++) { topK.insert(i); } for (int j = 1; j <= topK.currentSize; j++) { System.out.print(topK.items[j] + " "); } System.out.println(); } }
运行结果:
blog