#include<stdio.h>
#include<conio.h>
void heapsort(int [],int);
void createheap();
void main()
{
int k[10],i,n;
clrscr();
printf("\n ! ! ! ! ! ! ! ! ! ");
printf("\n INPUT ");
printf("\n enter how many elements ");
scanf("%d",&n);
printf("\n enter array elements");
for(i=0;i<n;i++)
scanf("%d",&k[i]);
printf("\n ! ! ! ! ! ! ! ! \n");
printf("\n * * * * * * * * *\n");
printf("\n output \n ");
printf("\n orginal list \n");
for(i=1;i<=n;i++)
printf("%4d",k[i]);
heapsort(k,n);
printf("\n* * * * * * * * * * *");
printf("\n sorted list \n");
for(i=1;i<=n;i++)
printf("%4d",k[i]);
printf("\n * * * * * * * * *\n");
getch();
return;
}
void createheap(int k[],int n)
{
int temp,key,i,q,j;
for(q=2;q<=n;q++)
{
i=q;
key=k[q];
j=i/2;
while((i>1)&&(key>k[j]))
{
temp=k[j];
k[j]=k[i];
k[i]=temp;
i=j;
j=i/2;
if(j<1)
j=1;
}
k[i]=key;
}
return;
}
void heapsort(int k[],int n)
{
int temp,q,i,j,key;
createheap(k,n);
for(q=n;q>=2;q--)
{
temp=k[q];
k[q]=k[i];
k[i]=temp;
i=1;
j=2;
key=k[i];
if((j+1)<q)
{
if(k[j+1]>k[j])
j++;
}
while((j<=(q-1))&&(k[j]>key))
{
temp=k[j];
k[j]=k[i];
k[i]=temp;
i=j;j=2*i;
if(j+1<q)
{
if(k[j+1]>k[j])
j++;
else
if(j>n)
j=n;
}
k[i]=key;
}
}
return;
}
#include<conio.h>
void heapsort(int [],int);
void createheap();
void main()
{
int k[10],i,n;
clrscr();
printf("\n ! ! ! ! ! ! ! ! ! ");
printf("\n INPUT ");
printf("\n enter how many elements ");
scanf("%d",&n);
printf("\n enter array elements");
for(i=0;i<n;i++)
scanf("%d",&k[i]);
printf("\n ! ! ! ! ! ! ! ! \n");
printf("\n * * * * * * * * *\n");
printf("\n output \n ");
printf("\n orginal list \n");
for(i=1;i<=n;i++)
printf("%4d",k[i]);
heapsort(k,n);
printf("\n* * * * * * * * * * *");
printf("\n sorted list \n");
for(i=1;i<=n;i++)
printf("%4d",k[i]);
printf("\n * * * * * * * * *\n");
getch();
return;
}
void createheap(int k[],int n)
{
int temp,key,i,q,j;
for(q=2;q<=n;q++)
{
i=q;
key=k[q];
j=i/2;
while((i>1)&&(key>k[j]))
{
temp=k[j];
k[j]=k[i];
k[i]=temp;
i=j;
j=i/2;
if(j<1)
j=1;
}
k[i]=key;
}
return;
}
void heapsort(int k[],int n)
{
int temp,q,i,j,key;
createheap(k,n);
for(q=n;q>=2;q--)
{
temp=k[q];
k[q]=k[i];
k[i]=temp;
i=1;
j=2;
key=k[i];
if((j+1)<q)
{
if(k[j+1]>k[j])
j++;
}
while((j<=(q-1))&&(k[j]>key))
{
temp=k[j];
k[j]=k[i];
k[i]=temp;
i=j;j=2*i;
if(j+1<q)
{
if(k[j+1]>k[j])
j++;
else
if(j>n)
j=n;
}
k[i]=key;
}
}
return;
}
No comments:
Post a Comment