package com.test.heap;
public class Heap {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = { 28 ,90,9,67,6,11,100,92,35,28,80,8,10,30,7,90,50,4};
//int[] array={10,30,7,90,50,4,5};
HeapsSort(array, array.length-1);
for (int i = 0; i < array.length; i++)
System.out.println(array[i]);
}
public static void HeapAdjust(int[] datas, int start, int end) {
int rc = datas[start];
int j = 2 * start;
while(j<=end){
//for ( ;j <= end; j = j * 2) {
if (j < end && datas[j] < datas[j + 1])
{
// System.out.println(j);
j++;
}
if (rc >= datas[j])
break;
datas[start] = datas[j];
// System.out.println(datas[j]);
start = j;
j = j*2;
}
datas[start] = rc;
}
public static void HeapsSort(int[] datas, int n) {
for (int i = n / 2; i >= 0; i--)
HeapAdjust(datas, i, n);
for (int i = n; i >= 0; i--) {
datas[0] = datas[i] + (datas[i] = datas[0]) * 0;
HeapAdjust(datas, 0, i - 1);
}
}
}