#include<stdio.h>
#include<conio.h>
int heapsize=10,ar[10];
void swap(int largest,int i)
{ int temp;
temp=ar[largest];
ar[largest]=ar[i];
ar[i]=temp;
}
void heapify(int i)
{ int l,r,largest,test=0;
l=i*2;
r=l+1;
if(l<=heapsize&&ar[i-1]<ar[l-1])
{largest=l;}
else
largest=i;
if(r<=heapsize&&ar[largest-1]<ar[r-1])
{largest=r;}
if(largest!=i)
{swap(largest-1,i-1);
heapify(largest);}
}
void build_heap()
{ int i;
for(i=5;i>0;i--)
heapify(i);
}
main()
{ printf("enter the values");
int i,j;
for(i=0;i<10;i++)
scanf("%d",&ar[i]);
build_heap();
//heapsort
//heapify(2);
for(i=10;i>1;i--)
{
swap(0,i-1);
heapsize--;
//j=heapsize>>2;
heapify(1);
}
for(i=0;i<10;i++)
printf("%d \n",ar[i]);
getch();
}
No comments:
Post a Comment
Have some problem with this code? or Request the code you want if you cant find it in Blog Archive.