#include <stdio.h>
#include <stdlib.h>
void makeHeap(int heap[],int,int);
int delete(int heap[]);
int no;
int main()
{
int heap[10],i,num,temp[10],n;
printf("Enter the number\n");
scanf("%d",&no);
n=no;
for(i=0;i<no;i++)
{
scanf("%d",&num);
makeHeap(heap,num,i);
}
printf("The heap element are\n");
for(i=0;i<no;i++)
printf("%d\t",heap[i]);
for(i=n-1;i>=1;i--)
temp[i]=delete(heap);
printf("\nThe sorted elements are\n");
for(i=0;i<n;i++)
printf("%d\t\t",temp[i]);
return 0;
}
void makeHeap(int heap[],int data,int index)
{
int parent,temp;
heap[index]=data;
while(index!=0)
{
parent=(index-1)/2;
if(heap[parent]>heap[index])
{
temp=heap[parent];
heap[parent]=heap[index];
heap[index]=temp;
}
index=parent;
}
}
int delete(int heap[])
{
int i,left,min,m,c;
int temp;
int value=heap[0];
heap[0]=heap[no-1];
no--;
i=0;
//Problem is coming in this while loop please help
while(i<no)
{
left=2*i+1;
if(heap[left]<heap[i]&&left<=no)
{
min=left;
temp=heap[left];
heap[left]=heap[i];
heap[i]=temp;
}
else
min=i;
if((heap[left+1]<heap[i])&&((left+1)<=no))
{
min=left+1;
temp=heap[left+1];
heap[left+1]=heap[i];
heap[i]=temp;
}
i=min;
}
return value;
}