多路归并排序
来源:互联网 发布:便携电脑推荐 知乎 编辑:程序博客网 时间:2024/06/06 02:15
题目要求:对整型链表数组(vector<list<int>> src)进行多路归并排序,得到一个有序链表(list<int> dest)。
代码实现:
#include<stdafx.h>#include <list>#include <vector>#include <iostream>#include <algorithm>using namespace std;list <int> merge(vector < list<int> > &src){size_t list_num = src.size();size_t total_elem = 0; //对每个链表排序for (size_t i = 0; i != list_num; i++) {src[i].sort();total_elem += src[i].size();} //堆的实现比较复杂,这里用最简单的方式,当链表的个数很大的时候,需要采用堆。list <int> dest;while (total_elem--) {int min= 0x7fffffff;int which= -1;for (int i = 0; i != list_num; i++) {if (src[i].empty())continue;if (src[i].front() < min) {min = src[i].front();which = i;}}if (min!= 0x7fffffff && which != -1) {src[which].pop_front();dest.push_back(min);}}return dest;}int main(){int n;vector < list<int> > src;cout<<"输入链表个数"<<endl;cin >> n; //链表个数for (int i = 0; i != n; i++) {int m;//第I个链表长度cout<<"输出第"<<i<<"个链表元素的个数"<<endl;cin >> m;list <int> tmp;//这里可以输入无序组数for (int j = 0; j != m; j++) {cout<<"输出第"<<i<<"个链表第"<<j<<"个元素个数"<<endl;int x;cin >> x;tmp.push_back(x);}src.push_back(tmp);}//输出所有链表for (int i = 0; i != n; i++) {for (list <int>::iterator it = src[i].begin(); it != src[i].end(); it++)cout << *it << " ";cout << endl;}//list <int> dest =merge(src);for (list <int>::iterator it = dest.begin(); it != dest.end(); it++)cout << *it << " ";cout << endl;getchar();return 0;}
0 0
- 多路归并排序
- 多路归并排序
- 多路归并排序
- 多路归并排序
- 多路归并排序
- 多路归并排序
- 数据结构 外部排序 多路归并排序
- 归并排序:二路归并
- 归并排序--二路归并
- 归并排序(2-路归并排序)
- 归并排序、二路归并排序
- 位图排序、多路归并排序-应对磁盘文件排序
- 二路和多路归并排序
- 多路归并排序【JAVA实现】
- 大数据多路归并排序
- 败者树 多路归并排序
- 大数据多路归并排序
- 大数据多路归并排序
- OC 的 #pragma mark - 在Swift 里的代替方法
- 给UIButton添加(类别)方法,方便批量创建button
- UIImage图片缩放到指定大小尺寸
- 探究javascript mvc(二)
- HEVC细琐杂项 持续更新~
- 多路归并排序
- 虚拟机复制后网络启动失败的解决办法
- Tokyo Cabinet和Tokyo Tyrant安装和测试
- 关于编写面向对象的时钟问题
- UVa12356 - Army Buddies(水题)
- java 10.20
- 百度地图的一些疑问
- Spark集群术语
- Vector简单使用