re: ICE Service使用方法简介 李伟 2011-07-11 09:59
支持@true
谢谢老大~~什么时候有空的话研究一下钓鱼的程序呀~~钓的我们累死~~
永远支持老大~~~~~~~
re: 我所理解的堆排序算法 李伟 2006-10-17 01:49
#define HEAP_MAX_LEN 100
#define DataType int
static DataType heap_table[HEAP_MAX_LEN+1];
static int current_size=0;

void minheap_init(void){
int i;
for(i=0;i<1+HEAP_MAX_LEN;++i)
heap_table[i]=0;
current_size = 0 ;
}

void minheap_filterdown(int start ,int end){
int i= start;//i and father while j as child
int j= i*2+1;
DataType temp= heap_table[i];
while(j<=end)
{
if (heap_table[j]>heap_table[j+1])j++;
if (temp<=heap_table[j])break;
else {
heap_table[i]=heap_table[j];i=j;j=2*j+1;
}
}
heap_table[i]=temp;
}
/*
void swap(DataType *a,DataType *b){
DataType t;
t=*a;*a=*b;*b=t;
}
*/
void minheap_filterup(int start){
int j=start;//i as child while j and father
int i=(j-1)/2;
DataType temp=heap_table[j];
while(j>0){
if (heap_table[i]<=temp)break;
else {heap_table[j]=heap_table[i];j=i;i=(i-1)/2;}
heap_table[j]=temp;
}
}

DataType minheap_fetch(void ){
DataType ret;
if (current_size==0){printf("heap is now empty!\n");}
ret=heap_table[0];heap_table[0]=heap_table[current_size-1];
current_size--;
minheap_filterdown(0,current_size-1);
return ret;
}

int minheap_insert(DataType x){
if (current_size>=HEAP_MAX_LEN){printf("heap is now full!\n");return 0 ;}
heap_table[current_size]= x;
minheap_filterup(current_size);
current_size++;
return 1;
}
int fill[]={8,9,10,7,6,5,2,3,4,1,29,45,67,890};
int main(){
int i=0;
int cnt = sizeof (fill)/4;
minheap_init();
while(i!=cnt){
if (minheap_insert(fill[i])==0)break;
++i;
}
i= 0;
while(i!=cnt){// printf("%d get one from minheap==>%d\n",current_size,heap_table[i]);

printf("get one from minheap==>%d\n",minheap_fetch());
++i;
}
getchar();getchar();getchar();
}

导航

<2026年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿

搜索

最新评论