标签:适合大量文件数据
#include <iostream>
using namespace std;
class MaxHash
{
public:
MaxHash(int n)
{
data = new int[n];
size = n;
}
void Insert(int a[], int n)
{
int i = 0;
for (; i < size; i++)//初始化数组
{
data[i] = a[i];
}
int j = size / 2;
while (j >= 0)//构造堆
{
InitHash(data,j);
j--;
}
while (i < n)//找出最小的size个数字
{
if (a[i] < data[0])
{
data[0] = a[i];
InitHash(data,0);
}
i++;
}
for (i = 0; i < size; i++)//打印找到的这size个数字
{
cout << data[i] << " ";
}
cout << endl;
}
void InitHash(int a[], int n)
{
int i = n;
int j = 2 * i + 1;
while (j< size)
{
int tmp = a[i];
if (j+1<size &&a[j] < a[j + 1])
j = j + 1;
if (a[i] < a[j])
{
a[i] = a[j];
a[j] = tmp;
}
i = j;
j = 2 * i + 1;
}
}
private:
int *data;
int size;
};
int main()
{
MaxHash mh(4);
int a[] = { 0,100, 88, 77, 66, 55, 44, 33, 22, 22, 11, 5, 4, 3, 2, 1, 1 };
mh.Insert(a, 17);
}
标签:适合大量文件数据
原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/46557697