import java.util.Arrays;
public class HeapSort {
public static void adjustMinHeap(int[] a, int pos, int len) {
int temp;
int child;
for (temp = a[pos]; 2 * pos + 1 <= len; pos = child) {
child = 2 * pos + 1;
if (child < len && a[child] > a[child + 1]) {
child++;
}
if (a[child] < temp) {
a[pos] = a[child];
} else {
break;
}
}
a[pos] = temp;
}
public static void myMinHeapSort(int[] a) {
int i;
int len = a.length;
for (i = len / 2 - 1; i >= 0; i--) {
adjustMinHeap(a, i, len - 1);
}
for (i = len - 1; i >= 0; i--) {
int tmp = a[0];
a[0] = a[i];
a[i] = tmp;
adjustMinHeap(a, 0, i-1);
}
}
public static void main(String[] args) {
int[] a = {5, 4, 9, 8, 6, 0, 1, 3, 2};
myMinHeapSort(a);
System.out.println(Arrays.toString(a));
}
}
08-02
5930
09-22
235