m路总数目为n (比败者树差些,比一般的要好)
#include <stdio.h>
#include <stdlib.h>
#define N 150
typedef struct data
{
int data;
int pos;
}data;
typedef struct wrap_data
{
int offset;
data *p;
}wrap_data;
wrap_data * min_heap[N]={0};
int count=1;
int left(i)
{
return 2*i;
}
int right(i)
{
return 2*i+1;
}
void insert(wrap_data *point_a)
//void insert(int a)
{
wrap_data * temp;
int pos_temp;
int p, num;
min_heap[count]=point_a;
num=count;
while(num/2)
{
p=num/2;
if((min_heap[num]->p+min_heap[num]->offset)->data<(min_heap[p]->p+min_heap[p]->offset)->data)
{
temp=min_heap[num];min_heap[num]=min_heap[p];min_heap[p]=temp;
}
else
{
break;
}
num=p;
}
count++;
if(count>N)count=N;
}
wrap_data * pop()
{
wrap_data * temp,*result=min_heap[1];
int exchange;
int label=1;
min_heap[label]=min_heap[count-1];
while(left(label)<=count-1)
{
if(right(label)<=count-1)
{
if((min_heap [left (label)]->p+min_heap[left(label)]->offset) ->data <
(min_heap [right (label)]->p+ min_heap[ right(label)]->offset)->data )
exchange=left (label) ;
else
exchange= right (label);
}
else
exchange=left (label) ;
if((min_heap[exchange]->p+ min_heap[exchange]->offset)->data<
(min_heap[label]->p+ min_heap[label]->offset)->data)
{
temp=min_heap[exchange];min_heap[exchange]=min_heap[label];min_heap[label]=temp;
}
else
break;
label=exchange;
}
count--;
return result;
}
int empty()
{
return !(count-1);
}
int main()
{
#define PATH 5
#define LENGTH 10
wrap_data *result;
int i=0,j=0;
wrap_data a[PATH]={0};
for(i=0;i<PATH;i++)
{
a[i].p=(data*) calloc (LENGTH , sizeof (data ));
a[i].offset=0;
for(j=0;j<LENGTH;j++)
{
(a[i].p+j)->data=(i*i+j)%40;
}
insert(&a[i]);
}
for(i=0;i<PATH;i++)
{
for(j=0;j<LENGTH;j++)
{
printf("%d ", (a[i].p+j)->data);
}
printf("\n");
}
while(!empty())
{
result=pop();
printf("%d ",(result->p+result->offset)->data);
result->offset++;
if ( result ->offset>=LENGTH)
{
}
else
{
insert(result);
}
// printf("%d \n",result->data);
}
}
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
4 5 6 7 8 9 10 11 12 13
9 10 11 12 13 14 15 16 17 18
16 17 18 19 20 21 22 23 24 25
0 1 1 2 2 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 9 10 10 10 11 11 12 12 13 13 14 15 16 16 17 17 18 18 19 20 21 22 23 24 25
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
本文介绍了一种使用堆数据结构实现的多路归并排序程序,适用于已排序的一路数据。程序中定义了数据结构,实现了插入和最小堆弹出操作,并通过示例展示了如何对多路数据进行归并。
&spm=1001.2101.3001.5002&articleId=6963125&d=1&t=3&u=f9bc83e3436946d19c6b0d6ff053ee8e)
342

被折叠的 条评论
为什么被折叠?



