#include "stdio.h"
int num[100] = {100, 3, 2, 0, 8, 9, 32, 12, 4, 1};
int n = 10;
void swap(int* a, int* b){
int t = *a;
*a = *b;
*b = t;
}
void ajast(int p, int ed){
if(2*p+1>=ed) return;
if(2*p+2<ed && num[2*p+2]<num[2*p+1] && num[p]>num[2*p+2]){
swap(&num[p], &num[2*p+2]);
ajast(2*p+2, ed);
}else if(num[p]>num[2*p+1]){
swap(&num[p], &num[2*p+1]);
ajast(2*p+1, ed);
}
}
void show(){
int i;
for(i=0; i<n; i++)
printf("%d ", num[i]);
printf("\n");
}
void insert(int val){
int t = val;
int q = n++;
int p = (q-1)/2;
do{
if(num[p]>t){
num[q] = num[p];
q = p;
p = (p-1)/2;
}else
break;
}while(!q);
num[q] = t;
}
void main(){
int i;
for(i=n-1; i>=0; i--)
ajast(i, n);
show();
insert(77); show();
insert(6); show();
for(i=0; i<n-1; i++){
swap(&num[0], &num[n-i-1]);
ajast(0, n-i-1);
}
show();
}