Heap Sort Without using Recursion

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void makeheap(int a[],int *);
int heapsort(int a[],int *);

void main()
{
int a[8]={23,55,46,35,10,90,84,31},b[8],t=7,i=0;

clrscr();
printf("before sorting\n");
makeheap(a,&t);

for(i=0;i<=t;i++)
{
printf("%d\t",a[i]);
}

printf("\nafter sorting\n");
for(i=0;i<=7;i++)
{
b[i]=heapsort(a,&t);
t--;
makeheap(a,&t);

}



for(i=0;i<=7;i++)
{
printf("%d\t",b[i]);
}
getch();
}
void makeheap(int a[],int *m)
{
int i=0,j,k,t;
int l;
l=(*m)/2 ;
for(j=0;j<l;j++)
{
for(k=0;k<2;i++,k++)
{
if(a[j]<a[i+1])
{
t=a[j];
a[j]=a[i+1];
a[i+1]=t;
}

if(a[0]<a[j])
{

t=a[j];
a[j]=a[0];
a[0]=t;
}
}
}


}
int  heapsort(int a[],int *p)
{
int t=0;

t=a[*p];
a[*p]=a[0];
a[0]=t;

return(a[*p]);

}

Comments

Popular posts from this blog

Pre-compiled Headers

CONNECTING WITH FTP-FILE TRANSFER PROTOCOL

PROBING THE INTERNET WITH TELNET: