The error occurs in maxheapify
when you check which of the children is larg
:
if (arr[l]>arr[i] && l<=size) ...
You've got the checks the wrong way round. When you read arr[l]
, l
might be out of bounds, which is a segmentation violation. If you check the bounds first like this:
if (l<=size && arr[l]>arr[i]) ...
the read access won't happen if l
is out of bounds. (Remember that the &&
and ||
operators short-circuit the evaluation: In a && b
, b
is never checked if a
is already false
, because there's no way the whole expression can be true.)
The same goes for the check of arr[r]
some lines after that.
Also, please include <stdlib.h>
and <time.h>
; they are needed for your randomisation code.