#include<stdio.h>
#include<conio.h>
ins(int tree[15],int n,int item)
{int ptr,par=0;
printf("\nitem here is%d \t\n",item);
n=n+1;
ptr=n;
while(ptr>1)
{par=ptr/2;
if(item<=tree[par])
{tree[ptr]=item;return;}
tree[ptr]=tree[par];
ptr=par;
}
tree[1]=item;
return;
}
del(int tree[15] ,int n,int item)
{int last,right,left,ptr=0;
item=tree[1];
last=tree[n];
ptr=1;left=2;right=3;
while(right<=n)
{
if(last>=tree[ptr])
{
tree[ptr]=last;
return;
}
if(tree[right]<=tree[left])
{ tree[ptr]=tree[left];ptr=left;}
else{tree[ptr]=tree[right];ptr=right;}
left=2*ptr;right=left+1;
}
if(left==n&&last<tree[left])
{ptr=left;
}
tree[ptr]=last;
return item;
}
int main()
{
int tree [15]={4,5,6,7,82,3,5,2,1,11,44,22,77,55,13};
int n =15;
int item;
int j=1,i;
printf("\nbefore sort\n");
for(i=1;i<n;i++)
printf("\n%d\t%d",i,tree[i]);
while(j<n-1)
{ ins(tree,j,tree[j+1]);
j++;
printf("\n\t\tj= %d",j);
}printf("j= %d",j);
for(i=1;i<15;i++)
{printf("%d\t%d\n",i,tree[i]);
}
j=n-1;
while(j>=1)
{item=del(tree,j,item);
tree[j+1]=item;
}
printf("\nafter sort\n");
for(i=1;i<15;i++)
{printf("%d\t%d\n",i,tree[i]);
}
getch();
return 0;
}
I am trying to heap sort an array of ints.
My code stops executing after the ins() function.
What is the fault here?