当前位置:网站首页>Open address method hash implementation - secondary detection method
Open address method hash implementation - secondary detection method
2022-07-30 02:50:00 【Chengqiuming】
一 点睛
The secondary detection method refers to the method of detecting by jumping back and forth,发生冲突时,向后 1 位探测,向前 1 位探测,向后 4 位探测,向前 4 位探测......Probe by jumping,避免堆积.
The incremental sequence of secondary detection is d=1,-1,4,-4,9,-9
二 需求
有一组关键字{14 36 42 38 40 15 19 12 51 65 34 25},若表长为 15,散列函数为hash(key) = key % 13,Then the second detection method can be used to deal with the conflict,构造散列表.
三 final detection result
哈希地址 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
关键字 | 65 | 14 | 40 | 42 | 15 | 19 | 34 | 36 | 51 | 38 | 12 | ||||
比较次数 | 1 | 1 | 2 | 1 | 4 | 2 | 1 | 1 | 3 | 1 | 2 |
四 实现
package hash;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
public class Hash {
static int m = 15; // 哈希表的表长
static int NULLKEY = 0;//单元为空的标记
static int HT[];
static int HC[];
// 哈希函数
static int H(int key) {
return key % 13;
}
static int Seconddetect(int HT[], int H0, int key, AtomicInteger cnt) {
int Hi;
for (int i = 1; i <= m / 2; ++i) {
int i1 = i * i;
int i2 = -i1;
cnt.getAndIncrement();
Hi = (H0 + i1) % m; // Calculate the next hash address according to the linear probing method Hi
if (HT[Hi] == NULLKEY)// 若单元 Hi 为空,则所查元素不存在
return Hi;
else if (HT[Hi] == key)//若单元Hi中元素的关键字为key
return Hi;
cnt.getAndIncrement();
Hi = (H0 + i2) % m; // Calculate the next hash address according to the linear probing methodHi
if (Hi < 0)
Hi += m;
if (HT[Hi] == NULLKEY) // 若单元 Hi 为空,则所查元素不存在
return Hi;
else if (HT[Hi] == key) // 若单元 Hi 中元素的关键字为 key
return Hi;
}
return -1;
}
static int SearchHash(int HT[], int key) {
// 在哈希表HT中查找关键字为 key 的元素,若查找成功,Returns the element number of the hash table,否则返回 -1
int H0 = H(key); // 根据哈希函数H(key)计算哈希地址
int Hi;
AtomicInteger cnt = new AtomicInteger(1);
if (HT[H0] == NULLKEY)// 若单元 H0 为空,则所查元素不存在
return -1;
else if (HT[H0] == key) { // 若单元H0中元素的关键字为 key,则查找成功
System.out.println("查找成功,比较次数:" + cnt);
return H0;
} else {
Hi = Seconddetect(HT, H0, key, cnt);//二次探测
if (HT[Hi] == key) { // 若单元 Hi 中元素的关键字为 key,则查找成功
System.out.println("查找成功,比较次数:" + cnt);
return Hi;
} else
return -1; //若单元Hi为空,则所查元素不存在
}
}
static boolean InsertHash(int HT[], int key) {
int H0 = H(key); // 根据哈希函数 H(key)计算哈希地址
int Hi = -1;
AtomicInteger cnt = new AtomicInteger(1);
if (HT[H0] == NULLKEY) {
HC[H0] = 1; // 统计比较次数
HT[H0] = key; // 单元 H0 为空,放入
return true;
} else {
Hi = Seconddetect(HT, H0, key, cnt);//二次探测
if ((Hi != -1) && (HT[Hi] == NULLKEY)) {
HC[Hi] = cnt.get();
HT[Hi] = key; // 若单元Hi为空,放入
return true;
}
}
return false;
}
static void print(int HT[]) {
for (int i = 0; i < m; i++)
System.out.print(HT[i] + "\t");
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x;
HT = new int[m];
HC = new int[m];
print(HT);
System.out.println("输入12个关键字,存入哈希表中:");
for (int i = 0; i < 12; i++) {
x = scanner.nextInt();
if (!InsertHash(HT, x)) {
System.out.println("Failed to create hash table!");
return;
}
}
System.out.println("输出哈希表:");
print(HT);
print(HC);
System.out.println("输入要查找的关键字");
x = scanner.nextInt();
int result = SearchHash(HT, x);
if (result != -1)
System.out.println("在第" + (result + 1) + "位置找到");
else
System.out.println("未找到");
return;
}
}
五 测试
绿色为输入,白色为输出.
边栏推荐
猜你喜欢
群论-Burnside引理与Polya定理 三千字
error: 'filesystem' is not a member of 'std'
Is it difficult for AI to land?Cloud native helps enterprises quickly apply machine learning MLOps
解决:Error while adding the mapper ‘interface to configuration. Error parsing Mapper XML
leetcode每天5题-Day01
华宝新能通过注册:拟募资近7亿 营收增加利润反而下降
【MySQL】SQL学习
奥比中光高级副总裁王兆民离职 董事会秘书暂未取得资格证
org.apache.ibatis.binding.BindingException Invalidbound statement (not found)的解决方案和造成原因分析(超详细)
多线程---初阶
随机推荐
快速入门jsp
【C语言刷LeetCode】1331. 数组序号转换(E)
博客搭建十:hugo博客添加友链
代码可读性,前置检查、注释及总结
【笔记】结巴分词绘制词云图
Drawing Problem Log
win11 自带远程桌面使用(包含非局域网使用以及win11升级为专业版)
乖宝宠物IPO过会:年营收25.75亿 KKR与君联是股东
判断Object是否依赖于名叫“XX“的资产
浏览器缓存机制
uni-app如何配置APP自定义顶部标题栏
binary search tree
实现批量导出功能
Unity Editor自定义一个记录Bug的窗口
Mysql中事务是什么?有什么用?
A plastic bottle of ocean "fantasy drifting"
票房破7.9亿美元,最近这部恐龙爽片你看了吗?
[3D检测系列-PointRCNN]复现PointRCNN代码,并实现PointRCNN3D目标检测可视化,包含预训练权重下载链接(从0开始以及各种报错的解决方法)
Kotlin接口
使用SqlSessionFactory工具类抽取