#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?

Dani AI

Generated

The symptom here is exactly what pointed out and what confirmed: the extraction loop never updates its loop counter, so the program falls into an infinite loop during the delete/sort phase. Fix that loop by decrementing j each iteration; for example:

for (j = n - 1; j >= 1; --j) {
    item = del(tree, j, item);
    tree[j + 1] = item;
}

A few additional points worth noting (these often produce subtle bugs in heap implementations and are not shown in the replies):

  • Be consistent about indexing. The code mixes a 1-based heap convention with a C initializer that fills indices 0..14. Either switch to 0-based indexing throughout (simpler in C) or allocate capacity+1 and explicitly use indices 1..size. Inconsistent indexing frequently leads to out-of-bounds access.
  • Use explicit function signatures and consistent return semantics. Declare ins/del with clear prototypes (for example, void heap_insert(int heap[], int *size, int item); and int heap_extract_root(int heap[], int *size);) so the current heap size is unambiguous and side effects are explicit.
  • Keep the heap type consistent (max-heap vs min-heap) and double-check comparison signs inside both insert and delete. Mismatched comparisons will silently produce a corrupted heap.
  • Compile with warnings enabled (-Wall -Wextra) and test on very small inputs. Printing the loop counter and heap array after each operation or stepping through with a debugger will quickly reveal infinite loops and index errors.

These changes both fix the immediate hang and make the implementation easier to reason about and maintain.

Recommended Answers

All 2 Replies

j=n-1;

while(j>=1)
{
    item=del(tree,j,item);
    tree[j+1]=item;
}

Where do you update j inside of this loop?

Where do you update j inside of this loop?

Yeah I figured it out,i had forgotten to decrement j.
Thanks...you are an angel

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.