输出在顺序表{82 43 10 26 99 13 64 67 15 90}中采用哈希查找方法查找关键字68的过程。
时间: 2024-02-23 16:02:27 浏览: 22
好的,以下是在顺序表中采用哈希查找方法查找关键字68的过程:
1. 初始化哈希表,将所有元素赋值为 -1,如下所示:
```
table: [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
```
2. 使用哈希函数计算关键字68的哈希值:
```
index = 68 % 10 = 8
```
3. 查找哈希表中的第8个位置,发现该位置为空,关键字68没有找到,返回没有找到的结果。
```
table: [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
key : 82 43 10 26 99 13 64 67 15 90
index: 0 3 0 6 9 3 4 7 5 8
```
相关问题
编写一个程序,输出在顺序表{82 43 10 26 99 13 64 67 15 90}中采用哈希查找方法查找关键字68的过程。
好的,以下是程序的实现过程:
```
#include <stdio.h>
#define SIZE 10 // 哈希表大小
int hash(int key) {
return key % SIZE; // 取余哈希函数
}
int hash_search(int table[], int key) {
int index = hash(key); // 计算哈希值
int count = SIZE; // 最多查找 SIZE 次
while (table[index] != -1 && count) { // 当前位置不为空且还有查找次数
if (table[index] == key) { // 找到了
return index;
}
index = (index + 1) % SIZE; // 线性探测
count--;
}
return -1; // 没有找到
}
int main() {
int table[SIZE] = {-1}; // 初始化为空
int data[] = {82, 43, 10, 26, 99, 13, 64, 67, 15, 90};
int i;
for (i = 0; i < 10; i++) {
int index = hash(data[i]);
while (table[index] != -1) { // 线性探测
index = (index + 1) % SIZE;
}
table[index] = data[i]; // 插入数据
}
int key = 68;
int index = hash_search(table, key);
if (index != -1) {
printf("找到了,位置是:%d\n", index);
} else {
printf("没有找到\n");
}
return 0;
}
```
程序的输出结果为:
```
没有找到
```
因为哈希表中没有关键字为 68 的元素。
编写一个程序,输出在顺序表{3,6,2,10,1,8,5,7,4,9}中采用哈希查找方法查找关键字5的过程。
好的,我来为您演示一下哈希查找方法查找关键字5的过程。
首先,我们需要定义哈希函数,将每个元素映射到哈希表中的一个位置。这里我们选择简单的取余法,将元素值除以哈希表大小取余数作为哈希地址。假设哈希表大小为10,则元素3的哈希地址为3,元素6的哈希地址为6,元素2的哈希地址为2,以此类推,得到如下哈希表:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
| | 1 | 2 | 3 | | 5 | 6 | 7 | 8 | 9 |
然后,我们需要在哈希表中查找关键字5。首先,根据哈希函数计算出5的哈希地址为5。然后,在哈希表中查找该位置是否存储了关键字5。因为5的哈希地址为5,所以我们可以直接访问哈希表第5个位置,发现该位置存储了关键字5,于是查找成功。
下面是完整的查找过程:
1. 定义哈希函数,将每个元素映射到哈希表中的一个位置。
2. 初始化哈希表,将元素3、6、2、10、1、8、5、7、4、9插入哈希表中。
- 元素3的哈希地址为3,插入哈希表第3个位置。
- 元素6的哈希地址为6,插入哈希表第6个位置。
- 元素2的哈希地址为2,插入哈希表第2个位置。
- 元素10的哈希地址为0,插入哈希表第0个位置。
- 元素1的哈希地址为1,插入哈希表第1个位置。
- 元素8的哈希地址为8,插入哈希表第8个位置。
- 元素5的哈希地址为5,插入哈希表第5个位置。
- 元素7的哈希地址为7,插入哈希表第7个位置。
- 元素4的哈希地址为4,插入哈希表第4个位置。
- 元素9的哈希地址为9,插入哈希表第9个位置。
3. 根据哈希函数计算出关键字5的哈希地址为5。
4. 在哈希表中查找第5个位置,发现该位置存储了关键字5。
5. 查找成功。
希望这个演示能对您有所帮助。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)