What is my mistake

Here is the code

Hi @hhiteshbansal_40d12e2460a1738e,

Your array is zero-based indexing.

int left=idx*2+1;
int right=idx*2+2; 

Also, p = c/2 -1 and while calling heapify you need to send y instead of p.

int p=c/2 -1;
for(int y=p;y>=0;y--){
     heapify(a,y); 
 }

no I still don`t understand my mistake

@hhiteshbansal_40d12e2460a1738e,

Your array is 0-based indexing ( index starts from 0) but while heafying you are treating your array as 1-based indexing,
left = 2*(idx)
right = 2*(idx) + 1

If idx equals 0 then, left = 0 and right = 1 which is wrong for the 0th index it’s left and right child is 1 and 2 respectively.

@hhiteshbansal_40d12e2460a1738e,
one more thing I am treating -1 as the part of array, are you considering -1?

yes i have taken -1 to block 0 index

oh okay sorry I treated -1 as the element.

@hhiteshbansal_40d12e2460a1738e,

heapify(a,p);

just change this line

heapify(a,y);

OUTPUT is coming out to be :- -1 1 4 5 6 20 8 9 10
it is neither a minheap nor a max heap what is now my mistake

image

This is min- heap.

ok i got it thankyou

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

you can check code pro zone for understanding coding languages.