2023-02-16 03:39:01 +08:00
|
|
|
|
# 哈希查找
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
!!! question
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
在数据量很大时,「线性查找」太慢;而「二分查找」要求数据必须是有序的,并且只能在数组中应用。那么是否有方法可以同时避免上述缺点呢?答案是肯定的,此方法被称为「哈希查找」。
|
|
|
|
|
|
2022-12-21 01:37:58 +08:00
|
|
|
|
「哈希查找 Hash Searching」借助一个哈希表来存储需要的「键值对 Key Value Pair」,我们可以在 $O(1)$ 时间下实现“键 $\rightarrow$ 值”映射查找,体现着“以空间换时间”的算法思想。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 算法实现
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
如果我们想要给定数组中的一个目标元素 `target` ,获取该元素的索引,那么可以借助一个哈希表实现查找。
|
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![哈希查找数组索引](hashing_search.assets/hash_search_index.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="hashing_search.java"
|
2023-02-07 04:43:52 +08:00
|
|
|
|
[class]{hashing_search}-[func]{hashingSearchArray}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="hashing_search.cpp"
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchArray}
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="hashing_search.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{}-[func]{hashing_search_array}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="hashing_search.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchArray}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="hashing_search.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchArray}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="hashing_search.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchArray}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="hashing_search.c"
|
2023-02-11 18:22:27 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchArray}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="hashing_search.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{hashing_search}-[func]{hashingSearchArray}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="hashing_search.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchArray}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="hashing_search.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchArray}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
再比如,如果我们想要给定一个目标节点值 `target` ,获取对应的链表节点对象,那么也可以使用哈希查找实现。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
![哈希查找链表节点](hashing_search.assets/hash_search_listnode.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="hashing_search.java"
|
2023-02-07 04:43:52 +08:00
|
|
|
|
[class]{hashing_search}-[func]{hashingSearchLinkedList}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="hashing_search.cpp"
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchLinkedList}
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="hashing_search.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{}-[func]{hashing_search_linkedlist}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="hashing_search.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchLinkedList}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="hashing_search.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchLinkedList}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="hashing_search.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchLinkedList}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="hashing_search.c"
|
2023-02-11 18:22:27 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchLinkedList}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="hashing_search.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{hashing_search}-[func]{hashingSearchLinkedList}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="hashing_search.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchLinkedList}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="hashing_search.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{}-[func]{hashingSearchLinkedList}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 复杂度分析
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-01-09 22:39:30 +08:00
|
|
|
|
**时间复杂度 $O(1)$** :哈希表的查找操作使用 $O(1)$ 时间。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-01-09 22:39:30 +08:00
|
|
|
|
**空间复杂度 $O(n)$** :其中 $n$ 为数组或链表长度。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 优点与缺点
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
在哈希表中,**查找、插入、删除操作的平均时间复杂度都为 $O(1)$** ,这意味着无论是高频增删还是高频查找场景,哈希查找的性能表现都非常好。当然,一切的前提是保证哈希表未退化。
|
|
|
|
|
|
|
|
|
|
即使如此,哈希查找仍存在一些问题,在实际应用中,需要根据情况灵活选择方法。
|
|
|
|
|
|
|
|
|
|
- 辅助哈希表 **需要使用 $O(n)$ 的额外空间**,意味着需要预留更多的计算机内存;
|
|
|
|
|
- 建立和维护哈希表需要时间,因此哈希查找 **不适合高频增删、低频查找的使用场景**;
|
|
|
|
|
- 当哈希冲突严重时,哈希表会退化为链表,**时间复杂度劣化至 $O(n)$** ;
|
|
|
|
|
- **当数据量很小时,线性查找比哈希查找更快**。这是因为计算哈希映射函数可能比遍历一个小型数组更慢;
|