-我在学习哈希算法是遇到了的问题,在学习哈希算法时遇到了问题,遇到了一些问题
a. Open Hash Table:
Bucket | Values |
---|---|
1 | 6173 |
2 | |
3 | 1323 |
4 | 4344 |
5 | |
6 | 1989 |
7 | 4199 |
8 | 9679 |
9 | |
0 | 4371 |
The hash table has 10 buckets numbered from 0 to 9. The hash function h(x) = x(mod 10) is applied to each value in the input, which determines the bucket in which the value will be stored. If the bucket is already occupied, we move to the next available bucket in a linear manner until we find an empty bucket. In the open hash table, the values are stored directly in the corresponding buckets.
b. Closed Hash Table using linear probing:
Bucket | Values |
---|---|
1 | 6173 |
2 | |
3 | 1323 |
4 | 4344 |
5 | 1989 |
6 | 4199 |
7 | 9679 |
8 | |
9 | 4371 |
0 |
In the closed hash table using linear probing, if a bucket is already occupied, we search the next available bucket in a linear fashion until we find an empty bucket. The values are stored in the next available bucket.
c. Closed Hash Table using quadratic probing:
Bucket | Values |
---|---|
1 | 6173 |
2 | |
3 | 1323 |
4 | 4344 |
5 | 1989 |
6 | 4199 |
7 | 9679 |
8 | |
9 | 4371 |
0 |
In the closed hash table using quadratic probing, if a bucket is already occupied, we search for the next available bucket using a quadratic function until we find an empty bucket. The values are stored in the next available bucket.
d. Closed Hash Table with second hash function h2(x) = 7 - (x mod 7):
Bucket | Values |
---|---|
1 | 6173 |
2 | 9679 |
3 | 1323 |
4 | 4344 |
5 | 1989 |
6 | 4199 |
7 | |
8 | |
9 | 4371 |
0 |
In the closed hash table with the second hash function h2(x) = 7 - (x mod 7), if a bucket is already occupied, we compute a new hash using the second hash function until we find an empty bucket. The values are stored in the next available bucket. In this example, the second hash function generates new hashes for values 6173 and 9679, which are stored in buckets 1 and 2 respectively.
哈希算法公式:hash(服务器的IP) % 2^32
上述公式的计算结果一定是 0 到 232-1 之间的整数,那么上图中的 hash 环上必定有一个点与这个整数对应,所以我们可以使用这个整数代表服务器,也就是服务器就可以映射到这个环上,假设我们有 ABC 三台服务器,那么它们在哈希环上的示意图如下: