最小堆
    最小堆为一颗排序的完全二叉树,根节点为最小值,每个节点都小于其作子节点或右子节点。

    1. final int[] arr = {1, 3, 5, 7, 2, 4, 6, 8};
    2. System.out.println(Arrays.toString(smallestKWithMinHead(arr, 4)));
    3. public static int[] smallestKWithMinHead(int[] arr, int k) {
    4. // 默认为最小堆
    5. PriorityQueue<Integer> queue = new PriorityQueue<>(k + 1);
    6. for (int i = 0; i < arr.length; i++) {
    7. queue.offer(arr[i]);
    8. }
    9. int[] data = new int[k];
    10. for (int i = 0; i < k; i++) {
    11. data[i] = queue.poll();
    12. }
    13. return data;
    14. }