Skip to content

Commit

Permalink
[notes][18_hashtable] hash function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam0205 committed Nov 30, 2018
1 parent 2d1d248 commit ec1cb44
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions notes/18_hashtable/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,19 @@
* 散列值/哈希值:Key 经过散列函数得到的数值

![](https://static001.geekbang.org/resource/image/92/73/92c89a57e21f49d2f14f4424343a2773.jpg)

本质:利用散列函数将关键字映射到数组下标,而后利用数组随机访问时间复杂度为 $\Theta(1)$ 的特性快速访问。

## 散列函数

* 形式:`hash(key)`
* 基本要求
1. 散列值是非负整数
1. 如果 `key1 == key2`,那么 `hash(key1) == hash(key2)`
1. 如果 `key1 != key2`,那么 `hash(key1) != hash(key2)`

第 3 个要求,实际上不可能对任意的 `key1``key2` 都成立。因为通常散列函数的输出范围有限而输入范围无限。

## 散列冲突¡

* 散列冲突:`key1 != key2``hash(key1) == hash(key2)`

0 comments on commit ec1cb44

Please sign in to comment.