解题思路:

排序方法:多路归并排序

每次将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路归并排序)的更多相关文章

  1. python 7:del 列表指定元素、list.pop(索引)、list.remove(元素值)(删除列表指定元素,且不可再使用;默认索引-1,弹出指定列表元素,可再使用;移除列表指定第一个元素)

    bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles) del bicycles[0] #删除指定列表元 ...

  2. 八大排序算法~简单选择排序【记录下标k变量的作用】

    八大排序算法~简单选择排序[记录下标k变量的作用] 1,思想:打擂台法,数组中的前n-1个元素依次上擂台"装嫩",后边的元素一个挨着一个不服,一个一个上去换掉它 2,优化:通过记录 ...

  3. 计数排序(O(n+k)的排序算法,空间换时间)

    计数排序就是利用空间换时间,时间复杂度O(n+k) n是元素个数,k是最大数的个数: 统计每个数比他小的有多少,比如比a[i]小的有x个,那么a[i]应该排在x+1的位置 代码: /* * @Auth ...

  4. Simplest Python K-Way Merging Sort|最简单的Python k路归并排序

    想做这个好长时间了,因为有一篇Dreamworks的论文<Coherent Out-of-Core Point-Based Global Illumination>提到了这个,一直没时间做 ...

  5. 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 ...

  6. LeetCode 23. 合并K个排序链表(Merge k Sorted Lists)

    题目描述 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [   1->4->5,   1->3->4,   2->6 ] 输出: ...

  7. 【13】堆排序 最小K个数

    题目 输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7.3.8这8个数字,则最小的4个数字是1.2.3.4. 收获 优先队列实现 (n1,n2)->n2-n1是 ...

  8. 算法-排序(1)k路平衡归并与败者树

    const int MaxValue=; //根据实际情况选择最大值 void kwaymerge(Element *r,int k){ int i,q; r=new Element[k]; //在败 ...

  9. 将一个整数数组先按照因子数量排序,再按照数字大小排序,输出第k个数

    同小米OJ比赛题:现在有 n 个数,需要用因子个数的多少进行排序,因子个数多的排在后面,因子个数少的排在前面,如果因子个数相同那么就比较这个数的大小,数大的放在后面,数小的放在前面.现在让你说出排序之 ...

随机推荐

  1. tableView里删除单元格

    tableView里删除单元格 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSInde ...

  2. C#解析复杂的Json成Dictionary<key,value>并保存到数据库(多方法解析Json 四)

    准备工作: 1.添加引用System.Web.Extensions, 2..net3.5+版本都有,如果VS2010找不到,在这个文件夹找:C:\Program Files\Reference Ass ...

  3. fetch VS AJAX

    fetch('https://mywebsite.com/endpoint/', { method: 'POST', headers: { 'Accept': 'application/json', ...

  4. Easy UI 遮罩(MASK)

    From :http://blog.csdn.net/luminji/article/details/16984839 Easy UI 的各类控件有些带了遮罩功能,如 DataGrid,可以这样使用: ...

  5. tornado介绍

    一.定义 tornado是一个异步非阻塞模型的服务器(tcp/http).web框架. 二.特性 1.高并发 原因:其一,网络事件循环部分根据操作系统选择最高效的,如Linux会是epoll: 其二, ...

  6. DotNetBar v12.7.0.2 Fully Cracked

    更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.7.0.2 如果遇到破解问题可以与我 ...

  7. JAVA小记

    关于重写equals()方法和重写toString()方法,一般来说,Objects的默认子类都重写了这两个方法,直接利用就行了: 对于用户自定义的类,如果要用到这两方法,就必须在程序中重写.

  8. switch… case 语句的用法(二)

    总结来说:switch的用法是判断case后面的表达式和switch后面的表达式是否相匹配,一旦case匹配,就会顺序执行后面的程序代码,而不管后面的case是否匹配,直到遇见break.都不匹配找d ...

  9. C++11的new concepts (move semantic)

    MoveConstructible 和MoveAssignable MoveConstructible Specifies that an instance of the type can be mo ...

  10. objectarx 卸载加载arx模块

    通常情况下,加载卸载arx模块是使用 APPLOAD命令 使用ObjectARX 代码操作,也非常简单,使用2个全局函数即可,参数为名字有扩展名 C++ int acedArxLoad( const ...