感谢:http://blog.csdn.net/mishifangxiangdefeng/article/details/7668486

声明:供自己学习之便而收集整理

题目:请给出一个时间为O(nlgk)、用来将k个已排序链表合成一个排序链表算法。此处n为所有输入链表中元素的总数。(提示:用一个最小堆来做k路合并)

算法:

step1:取每个链表的第一个元素,构造成一个含有k个元素的堆

step2:把根结点的值记入排序结果中。

step3:判断根结点所在的链表,若该链表为空,则go to step4,否则go to step5

step4:删除根结点,调整堆,go to step2

step5:把根结点替换为原根结点所在链表中的第一个元素,调整堆,go to step 2

//基于最小堆的K路归并算法
#include <iostream>
#include <ctime>
#include <limits>
using namespace std; typedef struct node{
int value;
node *next;
}node,*pNode; void insert_node(node *head, node *nenode)//直接插入法,插入新节点
{
node *cur=head;
if(head->next==NULL){
head->next=nenode;
}else{
while ((cur->next!=NULL)&&(cur->next->value<nenode->value)){
cur=cur->next;
}
nenode->next=cur->next;
cur->next=nenode;
}
} pNode delete_first_node(node *head)//删除链表第一个有效元素
{
//node *cur=head;
pNode min_value=NULL;
if(head->next!=NULL){
/*return ;
}else{*/
min_value=head->next;
head->next=head->next->next;
}
return min_value;//链表的最小值
} void min_heapify(pNode *a,int i,int n)//维护最小堆
{
int left=*i+;
int right=*i+;
int smallest=i;
if(left<n && a[smallest]->value>a[left]->value){
smallest=left;
}
if(right<n && a[smallest]->value>a[right]->value){
smallest=right;
}
if(smallest!=i){
pNode tmp=a[i];
a[i]=a[smallest];
a[smallest]=tmp;
min_heapify(a,smallest,n);
}
} void build_min_heapify(pNode *a,int n)//建立最小堆
{
for(int i=(n-)/;i>=;--i){
min_heapify(a,i,n);
}
} int heap_extract_min(pNode *a,int n)//获取堆的最小值,并更新堆
{
int min_value=a[]->value;
if(a[]->next!=NULL){
a[]=a[]->next;
}else{
a[]->value=numeric_limits<int>::max();
}
min_heapify(a,,n);
return min_value;
} int main()
{
srand(time(NULL));
int k;
while (k=(rand()%),k< || k>);//k个链表
int *size=new int[k];//每个链表的大小,不含首元素
node *ptr;//节点指针
pNode *pn=new pNode[k];//对k个链表初始化
for(int i=;i<k;++i){//k个链表的头节点
pn[i]=new node;//初始化第i个链表
pn[i]->value=numeric_limits<int>::min();
pn[i]->next=NULL;
while (size[i]=(rand()%),size[i]<);//第i个链表有size个元素
for(int j=;j<size[i];++j){
ptr=new node;
ptr->value=rand()%-;
ptr->next=NULL;
insert_node(pn[i],ptr);
}
} for(int i=;i<k;++i){//输出k个链表的元素
cout<<"list "<<i<<" has "<<size[i]<<" elements"<<endl;
node *cur=pn[i]->next;
while (cur->next!=NULL){
cout<<cur->value<<" -> ";
cur=cur->next;
}
cout<<cur->value;
cout<<endl;
}
cout<<endl; pNode *min_heap=new pNode[k];//排序堆,用来存储每个链表的第一个元素 for(int i=;i<k;++i){//删除首节点后,输出k个链表的元素
min_heap[i]=delete_first_node(pn[i]);//将最小元素存储在数组min_heap中
//cout<<min_heap[i]->value<<"\t";
/*while (pn[i]->next->next!=NULL){
cout<<pn[i]->next->value<<" -> ";
pn[i]=pn[i]->next;
}
cout<<pn[i]->next->value;
cout<<endl;*/
}
//cout<<endl; build_min_heapify(min_heap,k);//建立最小堆 int size_sum=;
for(int i=;i<k;++i){//获取k个堆的元素总数
size_sum+=size[i];
}
int min_val;//存储每次的最小值
for(int i=;i<size_sum;++i){
min_val=heap_extract_min(min_heap,k);
cout<<min_val<<"\t";
}
cout<<endl; delete [] size;
for(int i=;i<k;++i){
delete [] pn[i];
}
delete[]pn;
delete[] min_heap;
}

使用最小堆来完成k路归并 6.5-8的更多相关文章

  1. 多线程外排序解决大数据排序问题2(最小堆并行k路归并)

    转自:AIfred 事实证明外排序的效率主要依赖于磁盘,归并阶段采用K路归并可以显著减少IO量,最小堆并行k路归并,效率倍增. 二路归并的思路会导致非常多冗余的磁盘访问,两组两组合并确定的是当前的相对 ...

  2. 算法导论 6.5.9 堆实现K路归并问题

    问题: 设计一个时间复杂度为O(NlogK)的算法,它能够将K个有序链表合并为一个有序链表,这里的N为所有输入链表包含的总的元素个数 分析: 该问题为经典的利用堆完成K路归并的问题: 当K个序列满足一 ...

  3. k路归并(败者树,记录败者)

          败者树在外排序中用到,每加入一个数字时,调整树需要o(lgk),比较快.外排序过程主要分为两个阶段:(1)初始化各归并段写入硬盘,初识化的方法,可利用内排序方法还可以一种叫置换选择排序的方 ...

  4. Merge k Sorted Lists, k路归并

    import java.util.Arrays; import java.util.List; import java.util.PriorityQueue; /* class ListNode { ...

  5. HDU - 6041:I Curse Myself(Tarjan求环&K路归并)

    There is a connected undirected graph with weights on its edges. It is guaranteed that each edge app ...

  6. POJ 2442(优先队列 k路归并 堆)

    Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...

  7. 【二叉堆】k路归并问题(BSOJ1941)

    Description 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Ai*x^2+Bi*x+Ci(x∈N*).给定这些Ai.Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复 ...

  8. POJ-2442 Sequence K路归并问题

    题目链接:http://poj.org/problem?id=2442 问题一:K个有序表合成一个有序表,元素共有n个.用堆优化 问题二:两个序列的前n小的元素.堆优化. 这题就是问题二的扩展,每次处 ...

  9. Problem A: Assembly Required K路归并

    Problem A: Assembly Required Princess Lucy broke her old reading lamp, and needs a new one. The cast ...

随机推荐

  1. !! This tutorial was designed to help you with installation and configuration of OpenCV4Android SDK.

    ref: http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/O4A_SDK.html#running-o ...

  2. Backbone seajs demo2

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. php mysql_insert_id()

    mysql_insert_id mysql_insert_id()返回给定的 link_identifier中上一步 INSERT 查询中产生的 AUTO_INCREMENT 的 ID 号.如果没有指 ...

  4. Github原理

    See image below:

  5. JSON对象末尾多余逗号问题

    平时开发用的IE10,没发现这个问题,测试人员对系统兼容性测试时发现了在IE7下存在问题. 问题代码如下: var person = { name: "John", age: 25 ...

  6. URAL1057. Amount of Degrees(DP)

    1057 简单的数位DP  刚开始全以2进制来算的 后来发现要找最接近x,y值的那个基于b进制的0,1组合 #include <iostream> #include<cstdio&g ...

  7. Spring安全框架 Spring Security

    Spring Security 的前身是 Acegi Security ,是 Spring 项目组中用来提供安全认证服务的框架. Spring Security  为基于J2EE企业应用软件提供了全面 ...

  8. HDU 1372 (搜索方向稍有改变) Knight Moves

    其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...

  9. 【第八篇】mvc razor视图配置404 500页面

    记住是最外层的这个Web.config 在  <system.web> </system.web>节点里面配置 <customErrors defaultRedirect ...

  10. ASP.NET路由系统实现原理:HttpHandler的动态映射

    我们知道一个请求最终通过一个具体的HttpHandler进行处理,而我们熟悉的用于表示一个Web页面的Page对象就是一个HttpHandler,被用于处理基于某个.aspx文件的请求.我们可以通过H ...