top k 简介:在大量数据中找出重复次数最多的前K个。
问题分析:
听起来这个问题十分简单,只需对这些数据进行一次排序即可得到前K个。如果这样的话,首先得定义一个数据结构来保存这些数据,大量的数据会消耗过大的进程资源,甚至“耗尽”进程的资源。还有一个问题是排序的时间复杂度是非常高的,一般来说,较快的排序算法时间复杂度是O(n*log2n)。倘若数据过大,也就是n过大,时间复杂度也会是非常高的。为了提高程序执行效率,我们应该尽快的优化程序,使程序在个个情况下的复杂度达到理想化。
解决办法:1.数据量过大,导致进程资源被耗尽的问题:将大量数据平均写到多个文件当中,再找出每个文件中的top k并记录,再找出记录下来的前top k。则最终找到的就为整个大数据中的top k。
2.时间消耗过大:我们可以先将每个文件中的数据存入到map表中去。first:数据,second:重复的次数。再利用小根堆,先将前10个数据添加到map表中,然后依次比较剩下的数据的second如果大于map表中top的second,则将他更新到map表中。最终只需打印出小根堆所对应的前10个元素即可。
#include <iostream>
#include <map>
#include <string>
#include <string.h>
#include <hash_map>
#include <queue>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct Value
{
Value(int key = 0 )
:_key(key)
,_count(1)
{ }
bool operator>(const Value &src)const //凡是不修改对象的,一律写成常方法
{
return _count > src._count;
}
int _key;
int _count;
};
int main()
{
FILE* pf = fopen("data.txt","w");
for(unsigned int i=0;i<1000000;++i)
{
fprintf(pf, "%d ",rand()%500);
}
fclose(pf);
const int FILE_NUM = 200;
FILE* pfiles[FILE_NUM];
for(int i=0; i<FILE_NUM; ++i)
{
char buffer[50] = "data.txt";
char index[10] = {0};
itoa(i, index, 10);
strcat(buffer, index);
pfiles[i] = fopen(buffer,"w");
}
pf = fopen("data.txt", "r");
while(!feof(pf))
{
int data = 0;
fscanf(pf,"%d", &data);
int fileindex = data%FILE_NUM;
fprintf(pfiles[fileindex], "%d ", data);
}
for(int i=0;i<FILE_NUM;i++)
{
fclose(pfiles[i]);
}
fclose(pf);
for(int i=0; i<FILE_NUM; ++i)
{
char buffer[50] = "data.txt";
char index[10] = {0};
itoa(i, index, 10);
strcat(buffer, index);
pfiles[i] = fopen(buffer,"r");
}
vector<Value> last;//保存最终200个文件中的分别的重复最多的前10个元素
for(int i=0;i<FILE_NUM;i++)
{
vector<int> array;
while(!feof(pfiles[i]))
{
int data;
fscanf(pfiles[i], "%d", &data);
array.push_back(data);
}
hash_map<int,Value> map2;
while(!array.empty())
{
//map2[array[i]]++;
hash_map<int,Value>::iterator it = map2.find(array.back());
if(it == map2.end())
{
map2[array.back()] = Value(array.back());
}
else
{
it->second._count++;
}
array.pop_back();
}
priority_queue<Value,vector<Value>,greater<Value>> que;
hash_map<int,Value>::iterator it2 = map2.begin();
for(int i=0;it2 != map2.end();++it2,++i)
{
if(i<10)
{
que.push(it2->second);
}
else
{
if(it2->second > que.top())
{
que.pop();
//que.push(it2->second);
que.push(it2->second);
}
}
}
while(!que.empty())
{
last.push_back(que.top());
que.pop();
}
}
hash_map<int,Value> map3;
priority_queue<Value,vector<Value>,greater<Value>> que1;
int flag = 0;
while(!last.empty())
{
hash_map<int,Value>::iterator it = map3.find(last.back()._key);
if(it == map3.end())
{
map3[last.back()._key] = last.back();
}
else
{
it->second._count++;
}
last.pop_back();
}
hash_map<int,Value>::iterator it = map3.begin();
for(int i=0;it!=map3.end();it++,i++)
{
if(i<10)
{
que1.push(it->second);
}
else
{
if(it->second > que1.top())
{
que1.pop();
que1.push(it->second);
}
}
}
while(!que1.empty())
{
cout<<"first:"<<que1.top()._key<<" second:"<<que1.top()._count<<endl;
que1.pop();
}
for(int i=0;i<FILE_NUM;i++)
{
fclose(pfiles[i]);
}
return 0;
}