void h_adjust(void *base, uint elemsize, uint begin, uint end, compare_func cmp)
{
uint j;
void * ex = malloc(elemsize);
memcpy(ex, base+begin*elemsize, elemsize);
for(j = 2*begin+1; j <= end; j=2*j+1)
{
if(j=0)
break;
memcpy(base+begin*elemsize, base+j*elemsize, elemsize);
begin = j;
}
memcpy(base+begin*elemsize, ex, elemsize);
free(ex);
}
void hsort(void *base, uint nmemb, uint size, compare_func func)
{
int i;
//nmemb total count;
//mine: the last index;
void *ex = malloc(size);
--nmemb;
for(i = (nmemb-1)/2; i>=0; --i)
h_adjust(base, size, i, nmemb, func);
for(i = nmemb; i > 0; --i)
{
memcpy(ex, base, size);
memcpy(base, base+i*size, size);
memcpy(base+i*size, ex, size);
h_adjust(base, size, 0, i-1, func);
}
free(ex);
}