Untitled
unknown
plain_text
4 years ago
577 B
11
Indexable
//implement a heap using top down approach for min heap
#include<stdio.h>
void top_heap(int *h, int n);
void main()
{
int h[100], n,i;
printf("enter the number of elements= ");
scanf("%d", &n);
printf("enter the elements= ");
for(i=0;i<n;i++)
{
scanf("%d", &h[i]);
}
top_heap(h,n-1);
printf("elements: \n");
for(i=0;i<n;i++)
{
printf("%d - ",h[i]);
}
}
void top_heap(int *h, int n)
{
int i,j,k,key;
for(k=1;k<=n;k++)
{
i=k;
key = h[i];
j=(i-1)/2;
while(i>0 && key < h[j])
{
h[i]=h[j];
i=j;
j=(i-1)/2;
}
h[i]=key;
}
}Editor is loading...