/** * 最小的低k個數 * 1. 建一個大根堆 * 2. 若是比1個元素大,和第一個元素交換,調整堆 * 3. 返回堆頂元素,即第一個元素 * */ private static int topMinK(int[] nums, int k) { buildMaxHeap(nums, k); for (int i = k + 1; i <= nums.length; i++) { if(nums[i - 1] < nums[0]) { nums[0] = nums[i - 1]; downAdjust(nums, 1, k); } } return nums[0]; } public static void heapSort(int[] nums) { buildMaxHeap(nums, nums.length); System.out.println("after build heap:"); ArrayUtil.printIntArray(nums); for (int i = nums.length; i > 1; i--) { int temp = nums[i - 1]; nums[i - 1] = nums[0]; nums[0] = temp; downAdjust(nums, 1, i - 1); } } //10 8 5 12 9 private static void buildMaxHeap(int[] nums, int end) { for (int i = end; i >= 1; i--) { downAdjust(nums, i, end); } } private static void downAdjust(int[] nums, int j, int end) { while (j * 2 <= end) { int maxIndex = j; maxIndex = j * 2 <= end && nums[j - 1] < nums[j * 2 - 1] ? j * 2 : maxIndex; maxIndex = j * 2 + 1 <= end && nums[maxIndex - 1] < nums[j * 2] ? j * 2 + 1 : maxIndex; if (maxIndex != j) { int temp = nums[maxIndex - 1]; nums[maxIndex - 1] = nums[j - 1]; nums[j - 1] = temp; j = maxIndex; } //if j = j * 2; } //while }