有k个list列表, 各个list列表的元素是有序的,将这k个列表元素进行排序( 基于堆排序的K路归并排序)
解题思路:
排序方法:多路归并排序
每次将n个list的头元素取出来,进行排序(堆排序),最小元素从堆中取出后,将其所在list的下一个元素
放入堆中,调整堆序列。
函数实现原型:
void listnodesort(list<list<Node> >* listlistnode){}
#include <iostream>
#include <list> using namespace std; struct Node{
int value;
Node *next = NULL;
};
///堆排序(调整堆)
void HeapAdjust(Node* heap, int i, int length){
int min = i;
int lch = *i+;
int rch = *i+; if(i <= (length-)/){
if(lch < length && heap[min].value > heap[lch].value){
min = lch;
}
if(rch < length && heap[min].value > heap[rch].value){
min = rch;
}
if(min != i){
swap(heap[min], heap[i]);
HeapAdjust(heap, min, length);
}
}
}
///堆排序(建堆)
void BuildHeap(Node* heap, int length)
{
///要确定好是-2,还是-1
for(int i = (length-)/; i >=; i--){
HeapAdjust(heap, i, length);
}
}
///主要排序函数
void listnodesort(list<list<Node> >* listlistnode)
{
list<Node> listTotal;
int length = (*listlistnode).size();
Node* heap = new Node[length];
int i = ;
for(list<list<Node> >::iterator iter = (*listlistnode).begin(); iter !=(*listlistnode).end(); iter ++){
heap[i] = (*iter).front();
i++;
}
BuildHeap(heap, length);
while(length>){
listTotal.push_back(heap[]);
if(length == ){
break;
}
if(heap[].next == NULL){
heap[] = heap[length-];
length -=;
}
else{
heap[] = *(heap[].next);
}
HeapAdjust(heap, , length);
}
cout << "totalList: ";
for(list<Node>::iterator it=listTotal.begin(); it!=listTotal.end(); it++){
cout << (*it).value << " ";
}
cout << endl;
} list<list<Node> > buildListList(int sum, int interval)
{
list<list<Node> > listlistnode;
for(int i = ; i<interval; i++){
list<Node> listnode; ///mode 1: start
Node* node = new Node(); ///此处一定要用new分配一个内存,不能直接使用Node node;
int j = i;
while(j<sum){
Node* nodeNext = new Node();
node->value = j;
if(j+interval > sum){
node->next = NULL;
}
else
node->next = nodeNext;
listnode.push_back(*node);
node = nodeNext;
j+=interval;
}
///mode 1: end ///mode2:start
// int j = i;
// Node* head = NULL;
// Node* endp = NULL;
// while(j<sum){
// Node* node = new Node();
// node->value = j;
// node->next = NULL;
// if(head == NULL){
// head = node;
// endp = node;
// }
// else{
// endp->next = node;
// endp = node; ///此处也不可忘了
// }
// j=j+interval;
// }
// while(head != NULL){
// listnode.push_back(*head);
// head = head->next;
// }
/// mode 2:end listlistnode.push_back(listnode);
} return listlistnode;
} void print(list<list<Node> > *listlistnode) ///到底是传值还是传指针,后续代码块中的操作也要做相应变动;
{
for(list<list<Node> >::iterator iter=(*listlistnode).begin(); iter != (*listlistnode).end(); iter++){
list<Node> listnode = *iter; ///迭代器解地址后就是其内部所指内容
for(list<Node>::iterator it=listnode.begin(); it!=listnode.end(); it++){
cout << (*it).value << " ";
}
cout << endl;
}
} int main()
{
list<list<Node> > listlistnode = buildListList(,);
print(&listlistnode);
listnodesort(&listlistnode); ///根据函数的定义决定是传值还是传指针 cout << "Hello world!" << endl;
return ;
}
另外,还有一种更加简单的方法:借助map结构,因为map本身就是key有序的序列结构,所以将listlistnode中的所有元素依次加入map中即可完成排序。
map<int, int> mapNode;
for(list<list<Node> >::iterator iter=listlistnode.begin(); iter != listlistnode.end(); iter++){
list<Node> listnode = *iter;
for(list<Node>::iterator it=listnode.begin(); it!=listnode.end(); it++){
///如果不存在则插入,如果存在则key相应得value++即可。
if(mapNode.count((*it).value)==)
mapNode.insert(pair<int, int> ((*it).value,));
else{
mapNode[(*it).value]++;
}
}
} for(map<int, int>::iterator it = mapNode.begin(); it!=mapNode.end(); it++){
cout << it->first << " " << it->second << endl;
}
有k个list列表, 各个list列表的元素是有序的,将这k个列表元素进行排序( 基于堆排序的K路归并排序)的更多相关文章
- python 7:del 列表指定元素、list.pop(索引)、list.remove(元素值)(删除列表指定元素,且不可再使用;默认索引-1,弹出指定列表元素,可再使用;移除列表指定第一个元素)
bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles) del bicycles[0] #删除指定列表元 ...
- 八大排序算法~简单选择排序【记录下标k变量的作用】
八大排序算法~简单选择排序[记录下标k变量的作用] 1,思想:打擂台法,数组中的前n-1个元素依次上擂台"装嫩",后边的元素一个挨着一个不服,一个一个上去换掉它 2,优化:通过记录 ...
- 计数排序(O(n+k)的排序算法,空间换时间)
计数排序就是利用空间换时间,时间复杂度O(n+k) n是元素个数,k是最大数的个数: 统计每个数比他小的有多少,比如比a[i]小的有x个,那么a[i]应该排在x+1的位置 代码: /* * @Auth ...
- Simplest Python K-Way Merging Sort|最简单的Python k路归并排序
想做这个好长时间了,因为有一篇Dreamworks的论文<Coherent Out-of-Core Point-Based Global Illumination>提到了这个,一直没时间做 ...
- 4. Median of Two Sorted Arrays *HARD* -- 查找两个排序数组的中位数(寻找两个排序数组中第k大的数)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- LeetCode 23. 合并K个排序链表(Merge k Sorted Lists)
题目描述 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6 ] 输出: ...
- 【13】堆排序 最小K个数
题目 输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7.3.8这8个数字,则最小的4个数字是1.2.3.4. 收获 优先队列实现 (n1,n2)->n2-n1是 ...
- 算法-排序(1)k路平衡归并与败者树
const int MaxValue=; //根据实际情况选择最大值 void kwaymerge(Element *r,int k){ int i,q; r=new Element[k]; //在败 ...
- 将一个整数数组先按照因子数量排序,再按照数字大小排序,输出第k个数
同小米OJ比赛题:现在有 n 个数,需要用因子个数的多少进行排序,因子个数多的排在后面,因子个数少的排在前面,如果因子个数相同那么就比较这个数的大小,数大的放在后面,数小的放在前面.现在让你说出排序之 ...
随机推荐
- Genymotion常见问题整合与解决方案
常见问题1:Genymotion在开启模拟器时卡在了starting virtual device(注意只有tarting virtual device窗口,没有模拟器的黑屏窗口) 原因:Vir ...
- 2.精通前端系列技术之seajs和gruntJs结合开发(三)
1.我们先来了解下模块化历史 模块化历史 nodeJS的出现(http://nodejs.org/) commonJS规范(http://www.commonjs.org/) 浏览器JS的模块化? A ...
- mongo .update
db.classes.update({"count":{$gt:20}},{$set:{"name":"c4"}},false,false) ...
- 父元素与子元素之间的margin-top问题(css hack)(转载)
情况: 父元素的盒子包含一个子元素盒子,给子元素盒子一个垂直外边距margin-top,父元素盒子也会往下走margin-top的值,而子元素和父元素的边距则没有发生变化. 解决方法: 1.修改父元素 ...
- AngularJS结构简介
AngularJS是MVC架构,M是C里面的属性-值,C是js的class,V是DOM 各个关键特性的结构如下图所示: http://my.oschina.net/tommyfok/blog/2970 ...
- java.net.SocketException: Too many open files
1.ps -ef|grep java 2.lsof -p 32636 3.lsof -p 20812|wc –l 这个也可以看一个进程打开的文件数 4.ulimit –a c3p0官方提供了两个参 ...
- Android 移动缩放的ImageView
今天介绍一下Android中怎么实现ImageView的缩放和移动,自定义TouchImageView. public class TouchImageView extends ImageView { ...
- 我认为测试应该掌握的SQL语句
最近在学习Oracle,对测试人员而言必须掌握两种语言:第一种是DML,数据操纵语言 (Data Manipulation Language) 是SQL语言中,负责对数据库对象运行数据访问工作的指令集 ...
- AFNetworking、MKNetworkKit和ASIHTTPRequest对比
之前一直在使用ASIHTTPRequest作为网络库,但是由于其停止更新,iOS7上可能出现更多的问题,于是决定更换网络库. 目前比较流行的网络库主要有AFNetworking和MKNetworkKi ...
- new work
果不其然,还是电子工程师适合我.