【发布时间】:2015-03-21 15:30:40
【问题描述】:
我正在为这个范围内的存储元素做一个哈希表:2000000-20000000 个值。
例子: 17664658-8,7587458-8,7338375-4,5741259-2.....
在 100000 个元素的样本中,冲突的数量约为 23939,而在 1000000 个元素的样本中,冲突的数量约为 439870。我对哈希表了解不多,但这个冲突的数量并不多高的?
我读到在一个受控的数字范围内你可以有一个相当统一的好哈希函数,但不知道如何或从哪里开始,有什么建议吗?
这是我的哈希函数。
int hash(char* clave,int m) { //m is the size of the table (about the double of the elements to be stored)
int number=0,i=0;
///
while(isdigit(clave[i])){ //i don't use the last 2 characters.
number+=(clave[i++]-'0');
if(isdigit(clave[i]))
number*=10;
}
/// mutiplication method
float dis,R;
R=0.6106154;
dis = R*(number) - floor(R*(number));
int result = (int)(dis*m);
return result;
}
【问题讨论】:
-
你的哈希表有多大?你有没有机会使用二的幂?
-
@TheParamagneticCroissant 对于 100000 个元素,表格的大小为 178144。
-
顺便说一句,只是为了好玩,请尝试使用 178141 或 178151 的表大小(最接近的素数,以防您想知道)。