当前位置:网站首页>Hash table - Review
Hash table - Review
2022-06-27 19:58:00 【Lao Yan is trying】
1. Definition
Generally speaking , Hashes can be condensed into one sentence “ Convert an element to an integer through a function , So that the integer can uniquely represent this element .” The conversion function is called hash function H, in other words , If the element is key, Then the conversion is an integer H(key).
So for key Is an integer , What are the commonly used hash functions ? Generally speaking, there are the following :
① direct addressing : The identity transformation ( namely H(key)=key)
② Linear transformation method : namely H(key)=a*key+b;
③ Square with the middle method ( Rarely used ): take key The middle bits of the square of are used as hash value .
④ Division and remainder : hold key Divide by a number mod The remainder obtained as hash The method of value :H(key)=key%mod
Through this hash function , You can convert a large number to no more than mod The integer of , So you can use it as a viable array subscript . Obviously mod When it's a prime number ,H(key) Cover as much as possible [0,mod) Each number in the range .
2. Conflicts and solutions
But the division and remainder method may have two different numbers key1 and key2, Their hash The values are the same .key2 You can no longer use this location , Defecation conflict .
There are roughly three ways to resolve conflicts :
2.1 Linear exploration (Linear Probing)
When you get key Of hash value H(key), But the subscript in the table is H(key) The position of has been used by some other element , Then check the next position H(key)+1 Is it occupied , without , Just use this location ; Otherwise, continue to check the next position ( That is to say hash The value keeps increasing 1).
If the table length is exceeded during the inspection , So go back to the top of the table and continue the cycle , Until you find a place to use , Or find that all positions in the table have been used .
obviously , This practice tends to lead to clustering , That is, several positions in the table are used , This generation will reduce efficiency to some extent .
2.2 Square detection (Quadratic probing)
Avoid clustering , In the following order :
![]()
If H(key)+k^2 It exceeds the watch length TSize, Then put H(key)+k^2 For meter length TSize modulus ;
If H(key)+k^2<0, It will be (H(key)+k^2)%TSize + TSize)%TSize As a result ( It is equivalent to H(Key)-k^2 Keep adding one TSize Until a nonnegative number appears ).
2.3 Chain address ( Zipper method )
Different from the above two methods , The chain address method does not compute new hash value , It's about putting all the H(key) same key Connect into a single linked list .
This allows you to set an array Link, The scope is Link[0]~Link[mod], among Link[h] Deposit H(Key)=h A single linked list of , So when more Key words key Of hash All are h when , You can put these conflicts directly key straight .
Connect with a single linked list , At this point, you can traverse the single linked list to find all H(key)=h Of key.
2.4 Double hash
You need to use two hash functions , When passing through the first hash function H1(key) When the obtained address conflicts , Then use the second hash function H2(key) Calculate the address increment of the keyword . Its specific hash function form is as follows :

Initial detection position
. among i Is the number of conflicts , For the initial 0. In double hashing , At most m-1 This probe will traverse all the positions in the table , go back to
Location .
3. Hash lookup and performance analysis
I won't go into more details here , Relatively simple , Paste above



边栏推荐
- 刷题记录:Easy 数组(持续更新)
- Character interception triplets of data warehouse: substrb, substr, substring
- 经纬度分析
- Array exercises follow up
- Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
- 键入网址到网页显示,期间发生了什么?
- Source code analysis of golang map concurrent read / write problem
- Data intelligence enters the "deep water area", and data governance is the key
- 判断一个变量是数组还是对象?
- MASS幸运哈希游戏系统开发丨冲突解决方法(代码分析)
猜你喜欢
随机推荐
Mobile low code development theme month | visual development one click generation of professional source code
1029 Median
What is ssr/ssg/isr? How do I host them on AWS?
散列表(Hash)-复习
运算符的基础知识
Data intelligence enters the "deep water area", and data governance is the key
MySQL beginner benefits
SQL Server - window function - solve the problem of filtering consecutive n records
UE4实现长按功能
Bit. Store: long bear market, stable stacking products may become the main theme
ABAP随笔-通过api获取新冠数据
Erreur Keil de Huada Single Chip Computer La solution de Weak
券商经理的开户二维码开户买股票安全吗?有谁知道啊
Cdga | what is the core of digital transformation in the transportation industry?
数智化进入“深水区”,数据治理是关键
通过 Cargo 管理 Rust 项目
1028 List Sorting
Leetcode 821. 字符的最短距离(简单) - 续集
Doctoral Dissertation of the University of Toronto - training efficiency and robustness in deep learning
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献









