Sie sind auf Seite 1von 2

(a)

#include<stdio.h>
#include<conio.h>
void crheap(int [], int);
void processheap(int [],int);
int main(void)
{
int k[50],small,big,q,key,n,t,i;
clrscr();
#include <stdio.h>
int main(void) {
int clrscr();
int n;
int k[100];
int i;
printf("\n enter the no. of elements: ");
scanf(" %d",&n);
printf("\n Now enter the array elements: ");
for(i=1;i<=n;i++)
{scanf(" %d",&k[i]);}
crheap(int k,int n);
int processheap(int k,int n);
return(0);
}
void int crheap(int k[],int n)
{
int i,q, big,small,temp;
for(q=2;q<=n;q++)
{
small=q;
big=(int)small/2;
while(small >1 && k[small] > k[big])
{
temp=k[small];
k[small]= k[big];
k[big]=temp;
small=big;
big=(int)small/2;
if(big < 1)
big=1;
}
}
printf("\n Now the array in heap form is: ");
for(i=1;i<=n;i++)
printf(" %3d",k[i]);
}
return 0;
}
return 0;
}
(b)
/* heapsort function */

void processheap(int k[],int n)


{
int current,big,small,i,maxnodes;
for(maxnodes=n;maxnodes>=2;maxnodes--)
{
current=k[maxnodes];;
k[maxnodes]=k[1];
/* change array to be a heap of size n-1 */
big=1
if (maxnodes-1 >= 3 && k[3] > k[2])
small=3;
else
small = 2;
/* move keys upwards to find place for current */
while (small<=maxnodes-1 && k[small]>=current)
{
k[big]=k[small];
big=small;
small=small*2;
if(small+1 <= maxnodes-1 && k[small+1] > k[small])
small = small + 1;
}
k[big]=current;
}
printf("\n The sorted array is : ");
for(i=1;i<=n;i++)
printf("%4d",k[i]);
getch();
}

Das könnte Ihnen auch gefallen