当前位置:网站首页>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



边栏推荐
- 从感知机到前馈神经网络的数学推导
- Redis cluster Series III
- Is the account opening QR code given by CICC securities manager safe? Who can I open an account with?
- Is it safe to buy stocks online and open an account?
- ABAP-CL_OBJECT_COLLECTION工具类
- qt中文乱码
- Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
- Embracing cloud Nativity: Practice of Jiangsu Mobile order center
- 使用MySqlBulkLoader批量插入数据
- 【help】JVM的CPU资源占用过高问题的排查
猜你喜欢

SQL Server - Window Function - 解决连续N条记录过滤问题

数智化进入“深水区”,数据治理是关键

Data intelligence enters the "deep water area", and data governance is the key

UE4:Build Configuration和Config的解释

Summary of submarine cable detection technology

Data intelligence enters the "deep water area", and data governance is the key

binder hwbinder vndbinder

GIS遥感R语言学习看这里

键入网址到网页显示,期间发生了什么?

Error reported by Huada MCU Keil_ Weak's solution
随机推荐
实战回忆录:从Webshell开始突破边界
Photoshop layer related concepts layercomp layers move rotate duplicate layer compound layer
MASS幸运哈希游戏系统开发丨冲突解决方法(代码分析)
1028 List Sorting
Bit. Store: long bear market, stable stacking products may become the main theme
UE4:Build Configuration和Config的解释
Kotlin微信支付回调后界面卡死并抛出UIPageFragmentActivity WindowLeaked
[bug] there is an error uploading the picture (413 request entity too large)
键入网址到网页显示,期间发生了什么?
Summary of submarine cable detection technology
买股票在券商经理的开户链接上开户安全吗?求大神赐教
【云驻共创】 什么是信息化?什么是数字化?这两者有什么联系和区别?
Crontab's learning essays
Enumeration and control flow operation in rust
经纬度分析
循环遍历及函数基础知识
网络上开户买股票是否安全呢?刚接触股票,不懂求指导
Workflow automation low code is the key
NVIDIA Clara-AGX-Developer-Kit installation
429-二叉树(108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树、 106.从中序与后序遍历序列构造二叉树、235. 二叉搜索树的最近公共祖先)