就是最大堆的条件反过来
package heap;
import java.util.Arrays;
public class SmallHeap {
public static void main(String[] args) {
SmallHeap smallHeap = new SmallHeap();
int[] a = new int[]{5, 4, 3, 2, 1, 10, 20, 41, 15, 0};
smallHeap.sort(a);
System.out.println(Arrays.toString(a));
}
public void sort(int[] arr) {
for (int i = arr.length / 2 - 1; i >= 0; i--) {
adjustHeap(arr, i);
}
}
public void adjustHeap(int[] arr, int index) {
int temp = arr[index];
int cIndex = 2 * index + 1;
if (cIndex > arr.length) return;
if (cIndex + 1 < arr.length && arr[cIndex] > arr[cIndex + 1]) {
cIndex++;
}
if (arr[index] > arr[cIndex]) {
arr[index] = arr[cIndex];
arr[cIndex] = temp;
}
adjustHeap(arr, cIndex);
}
}