java - 如何让我的线性探测散列函数更高效?
问题描述
我在这里努力查看我的线性探测技术是否正确以及它是否有效。我有什么办法可以提高效率吗?
static void enterValues(int values[], int hashTable[])
{
for(int i = 0; i < values.length; i++){
int k = hashFunction(values[i]);
if(hashTable[k]== 0)
hashTable[k] = values[i];
else{
boolean b = false;
int counter = k%hashTable.length+1;
if(counter >= hashTable.length)
counter = 0;
while (!b) {
if (hashTable[counter] == 0) {
hashTable[counter] = values[i];
b = true;
} else {
counter = counter % hashTable.length+1;
}
}
}
}
}
static int hashFunction(int value)
{
return value % 10;
}
int values[] = {4371,1323,6173,4199,4344,9679,1989};
大小为 10 的哈希集的输出是
9679,
4371,
1989,
1323,
6173,
4344,
0,
0,
0,
4199
谢谢你看
解决方案
It is incorrect. Consider what happens if value[i]
is zero:
if (hashTable[k] == 0) {
hashTable[k] = values[i];
} else { .... }
Since you are using zero in the hashtable to mean that the entry is not used, and you are also assigning values directly into the table, your code cannot distinguish a zero value from an empty entry.
推荐阅读
- python - Music21 Python 库将只输出钢琴声音
- sql-server - 使用 OVER 或 LAG 函数计算 ROLLING 5 天期间的订单数量?
- django - 有没有办法将表单数据从一个url发布到另一个url,然后根据表单数据渲染模板
- php - 未知的mysql单元条目
- typescript - 用于 Storybook 的 Webpack:找不到具有 .web、.ios 或 .android 扩展名的 ts 文件的模块
- ios - 我该怎么做:库的回调等到用户从弹出窗口中选择然后返回
- php - 无法从下拉菜单中获取 PHP 切换案例中的用户输入
- python - 从对创建一个元组
- wordpress - Wordpress 无法加载脚本
- c# - 确定我收到的 CSV 文件版本/类型,以便我可以将其添加到模型中