当前位置:网站首页>Open address method hash implementation - linear detection method
Open address method hash implementation - linear detection method
2022-07-30 03:04:00 【Chengqiuming】
一 点睛
The linear probing method is the simplest development address method,The incremental sequence of linear probing is d=1,2...m-1
二 需求
有一组关键字{14 36 42 38 40 15 19 12 51 65 34 25},若表长为 15,散列函数为hash(key) = key % 13,Then the linear detection method can be used to deal with the conflict,构造散列表.
三 最终探测结果
哈希地址 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 15 |
关键字 | 65 | 14 | 40 | 42 | 15 | 25 | 19 | 34 | 36 | 38 | 12 | 51 | |||
比较次数 | 1 | 1 | 2 | 1 | 3 | 9 | 1 | 1 | 1 | 1 | 2 | 3 |
四 实现
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 Linedetect(int HT[], int H0, int key, AtomicInteger cnt) {
int Hi;
for (int i = 1; i < m; ++i) {
cnt.getAndIncrement();
Hi = (H0 + i) % m; // 按照线性探测法计算下一个哈希地址 Hi
if (HT[Hi] == NULLKEY)
return Hi; // 若单元 Hi 为空,则所查元素不存在
else if (HT[Hi] == key)
return Hi; // 若单元 Hi 中元素的关键字为 key
}
return -1;
}
static int SearchHash(int HT[], int key) {
// 在哈希表HT中查找关键字为 key 的元素,若查找成功,返回哈希表的单元标号,否则返回 -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 = Linedetect(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 = Linedetect(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("创建哈希表失败!");
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;
}
}
五 测试结果
边栏推荐
- 复旦-华盛顿大学EMBA科创的奥E丨《神奇的材料》与被塑造的我们
- 【C语言刷LeetCode】592. 分数加减运算(M)
- 【C语言刷LeetCode】2295. 替换数组中的元素(M)
- 厉害,腾讯技术专家手撸Redis技术笔记,下载量已突破30W
- 【C语言刷LeetCode】1331. 数组序号转换(E)
- centOS安装MySQL详解
- Solve The problem of Google browser cross-domain has had been blocked by CORS policy: The request The client is not a secure context and The resou
- Embedded SIG | 分布式软总线
- 解决谷歌浏览器跨域问题has been blocked by CORS policy: The request client is not a secure context and the resou
- 【ModelArts系列】华为ModelArts Notebook训练yolov3模型(开发环境)
猜你喜欢
随机推荐
【高性能计算】openMP
Leetcode.19 删链表倒数第 N 个结点(栈/先后指针)
NLP Natural Language Processing (2)
Ansible简介(详细)特性+优点+设计理念+应用领域+系统架构+工作原理+任务执行流程
Hyperchain超块链创始人史兴国接受21世纪经济报道采访,解读上海NFT新规及数藏发展
un7.29:Linux——centos中如何安装与配置redis?
Oracle 进程数和会话数的关系
解决导航栏变黑色
3.nodejs--modularization
HCIP实验(05)OSPF综合实验
Three years of experience will only be a little bit (functional testing), and you may not even be able to find a job after resigning.
Zero code tools recommended - HiFlow
B. Inflation-Educational Codeforces Round 103 (Rated for Div. 2)
雪花是否一样问题
JIT VS AOT
华宝新能通过注册:拟募资近7亿 营收增加利润反而下降
One book 1922 - table tennis
ESP8266 +0.96“ I2C OLED 表盘时钟
Awesome, Tencent technical experts handed Redis technical notes, and the download volume has exceeded 30W
中级-面试题目整理