当前位置:网站首页>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
边栏推荐
- 作为软件工程师,给年轻时的自己的建议(下)
- Pyhton爬取百度文库文字写入word文档
- openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
- 1027 Colors in Mars
- 可观测,才可靠:云上自动化运维CloudOps系列沙龙 第一弹
- 金鱼哥RHCA回忆录:DO447管理项目和开展作业--创建作业模板并启动作业
- Leetcode 989. 数组形式的整数加法(简单)
- As a software engineer, give advice to young people (Part 2)
- 429-二叉树(108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树、 106.从中序与后序遍历序列构造二叉树、235. 二叉搜索树的最近公共祖先)
- Doctoral Dissertation of the University of Toronto - training efficiency and robustness in deep learning
猜你喜欢
连接集成开发专题月 | 企业主数据治理的驱动因素
Accumulating power in the middle stage, UFIDA IUAP builds a new base for digital intelligence of social enterprises
可观测,才可靠:云上自动化运维CloudOps系列沙龙 第一弹
Bit. Store: long bear market, stable stacking products may become the main theme
在线文本按行批量反转工具
External interrupt experiment based on stm32f103zet6 library function
Doctoral Dissertation of the University of Toronto - training efficiency and robustness in deep learning
【debug】平台工程接口调试
Crawl national laws and Regulations Database
Web APls 阶段——第十四节——本地存储
随机推荐
Common shell script commands (4)
redis集群系列三
1024 Palindromic Number
Labelimg usage guide
MySQL beginner benefits
带你认识图数据库性能和场景测试利器LDBC SNB
華大單片機KEIL報錯_WEAK的解决方案
Leetcode 1381. 设计一个支持增量操作的栈
判断一个变量是数组还是对象?
Workflow automation low code is the key
Memoirs of actual combat: breaking the border from webshell
Is it safe to buy stocks online and open an account?
ABAP-CL_OBJECT_COLLECTION工具类
Summary of submarine cable detection technology
数智化进入“深水区”,数据治理是关键
Photoshop-图层相关概念-LayerComp-Layers-移动旋转复制图层-复合图层
External interrupt experiment based on stm32f103zet6 library function
券商经理的开户二维码开户买股票安全吗?有谁知道啊
One to one relationship
金鱼哥RHCA回忆录:DO447管理项目和开展作业--创建作业模板并启动作业