3static void siftdown(
void *base,
size_t _size,
4 int (*compar)(
const void *,
const void *),
5 void (*swap)(
void *,
void *),
8 long size = _size, root, child;
10 for ( root = start; root * 2 + size < stop; root = child )
12 child = root * 2 + size;
14 if ( (child + size < stop) &&
15 (compar(base + child, base + child + size) < 0) )
18 if ( compar(base + root, base + child) >= 0 )
21 swap(base + root, base + child);
25void heapsort(
void *base,
size_t nmemb,
size_t _size,
26 int (*compar)(
const void *,
const void *),
27 void (*swap)(
void *,
void *))
29 long size = _size, total = size * nmemb, idx;
31 for ( idx = (nmemb/2 - 1) * size; idx >= 0; idx -= size )
32 siftdown(base, _size, compar, swap, idx, total);
34 for ( idx = total - size; idx > 0; idx -= size )
36 swap(base, base + idx);
37 siftdown(base, _size, compar, swap, 0, idx);
void heapsort(void *base, size_t nmemb, size_t _size, int(*compar)(const void *, const void *), void(*swap)(void *, void *))
static void siftdown(void *base, size_t _size, int(*compar)(const void *, const void *), void(*swap)(void *, void *), long start, long stop)
Common declarations for all tests.