有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 个数,需要用因子个数的多少进行排序,因子个数多的排在后面,因子个数少的排在前面,如果因子个数相同那么就比较这个数的大小,数大的放在后面,数小的放在前面.现在让你说出排序之 ...
随机推荐
- Hduacm—5497
#include <cstring> #include <cstdio> #include <iostream> using namespace std; type ...
- 获取checkbox数组 里面的值
echo '<td class="text-left"><input name="tids[]" type="checkbox&q ...
- Div层的展开与收缩的代码
<html> <head> <title>div展开收缩代码</title> <style> * { margin:0; padding:0 ...
- iOS Android图标生成器PHP
<?php //修改为你想要的大小 //$sizes = array(16,29,32,36,48,50,57,58,72,76,96,100,114,120,128,144,152); $si ...
- POJ 1850 Code 字符串 难度:1
题意: 1 如果是严格升序的字母字符串,那么可以输出非0解码,否则不能译码输出0 2 字符串解码 遵循递增原则,其值为 到现在为止的所有按字母序小于该字符串的数量 + 1; #include < ...
- c# datagridview按条件搜索查询过滤
DataView的RowFilter 实现过滤 根据文本框文字对datagridview的数据进行模糊查询, 其实也就是一个过滤 string qymc = textBox1.Text.ToStrin ...
- TADOTable 用过滤事件 后 记录数据和 记录的内容
用 过滤事件,过滤后 ADOTbTrade.RecordCount 是总数, 但是,记录内容是 过滤后的 ADOTbTrade.First; while not ADOTbTrade.Eof do b ...
- HTML--8Window.document对象
1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunme ...
- poj1141 区间dp+路径
//Accepted 176 KB 47 ms //感谢大神们为我们这群渣渣铺平前进的道路!! //用scanf("%s",s)!=EOF WA到死 #include <cs ...
- bistu新生-1004
#include "stdio.h"#include "stdlib.h"#include "math.h"int cmp(const vo ...