package nomal;
public class HeapSort {
public static void main(String[] args) {
int[] arr = {23,3, 54, 12, 7, 99, 88, 65};
heapSort(arr);
for (int i : arr) {
System.out.print(i + " ");
}
}
private static void heapSort(int[] arr) {
for (int i = (arr.length - 1) / 2; i >= 0; i--) {
adjustHeap(arr, i, arr.length);
}
for (int i = arr.length - 1; i > 0; i--) {
int temp = arr[i];
arr[i] = arr[0];
arr[0] = temp;
adjustHeap(arr, 0, i);
}
}
private static void adjustHeap(int[] arr, int parent, int length) {
int temp = arr[parent];
int lChild = parent * 2 + 1;
while (lChild < length) {
int rChild = lChild + 1;
if (rChild < length && arr[lChild] < arr[rChild]) {
lChild++;
}
if (temp >= arr[lChild]) {
break;
}
arr[parent] = arr[lChild];
parent = lChild;
lChild = lChild * 2 + 1;
}
arr[parent] = temp;
}
}