I am trying to write a heap sort function using the prototype:
where base is a pointer to the first element
heap_sort(void *base, size_t nel, size_t width, int(*compar)(const void *, const void *));
nel is the number of elements to be sorted
width is the size of each element
and compar is a standard comparison function
(the same prototype for the library function qsort)
I have successfully written a swap_data function and the necessary comparison functions.
I was wondering if anyone can give me some hints on how to do things like setting child = 2 * parent + 1 using these definitions when parent and child are void* items. I keep getting "assignment makes pointer from integer without a cast" and "invalid use of unary *" errors.
I posted a generic quicksort implementation a while back. The quality is debatable (written in a hurry), but it should give you some ideas on how to do your heapsort implementation.
Thanks, that cleared up some things, but I still cannot find the child nodes. The children appear at 2*parent+1 in a heap as far as I know, but when parent is void* (or unsigned char*) rather than being an int in an array, I just don't know how to find the child.
Don't worry, I worked everything out, yay!
For anyone that was interested, instead of doing:
I just did:
child = 2*parent + width
which makes sense if you think about it long enough, and draw diagrams and stuff :)
child = parent + (parent - base) + width
Out of curiousity, do you go to Melb Uni? :)
Well because we nearly got the same question and at the same time for 1 of our assignments.
Yep, pretty easy to guess I suppose, damn sorting project took me ages. Still, I learned a hell of a lot from it so it's not all bad.
Haha yer the report was a pain to write :p