3
25
2012
0

C函数堆排序算法

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); }
Category: 未分类 | Tags: | Read Count: 1420

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com