43 #define SWAP(a, b, count, size, tmp) \ 55 #define COPY(a, b, count, size, tmp1, tmp2) \ 73 #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) \ 75 for(par_i = initval; (child_i = par_i * 2) <= nmemb; par_i = child_i) \ 77 child = base + child_i * size; \ 78 if(child_i < nmemb && compar(child, child + size) < 0) \ 83 par = base + par_i * size; \ 84 if(compar(child, par) <= 0) \ 86 SWAP(par, child, count, size, tmp); \ 107 #define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) \ 109 for(par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) \ 111 child = base + child_i * size; \ 112 if(child_i < nmemb && compar(child, child + size) < 0) \ 117 par = base + par_i * size; \ 118 COPY(par, child, count, size, tmp1, tmp2); \ 123 par_i = child_i / 2; \ 124 child = base + child_i * size; \ 125 par = base + par_i * size; \ 126 if(child_i == 1 || compar(k, par) < 0) \ 128 COPY(child, k, count, size, tmp1, tmp2); \ 131 COPY(child, par, count, size, tmp1, tmp2); \ 142 int heapsort(vbase, nmemb, size, compar)
void* vbase;
144 int (*compar)(
const void*,
const void*);
147 char tmp, *tmp1, *tmp2;
148 char *base, *k, *p, *t;
174 base = (
char*)vbase - size;
176 for(l = nmemb / 2 + 1; --l;)
177 CREATE(l, nmemb, i, j, t, p, size, cnt, tmp);
186 COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2);
187 COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2);
189 SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2);
void free(void *ptr)
Deallocates allocated memory space.
#define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2)
#define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp)
void * malloc(size_t size)
Allocates size bytes of uninitialized storage.
#define COPY(a, b, count, size, tmp1, tmp2)
int heapsort(void *vbase, size_t nmemb, size_t size, int *compar)