File size: 441 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public class MaxHeap{
static void Insert(int H[], int n){
int i = n, temp;
// it stored the last element
temp = H[i];
while(i>1 && temp>H[i/2]){
H[i]=H[i/2];
i = i/2;
}
H[i] = temp;
}
public static void main(String[] args){
int H[] = {30,20,15,5,10,12,6,40};
for(int i=2;i<=7;i++){
Insert(H,i);
}
for(int i=1;i<=7;++i){
System.out.print(H[i]+" ");
}
}
}
|