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



边栏推荐
- Character interception triplets of data warehouse: substrb, substr, substring
- 在线文本按行批量反转工具
- GIS遥感R语言学习看这里
- Solution of adding st-link to Huada MCU Keil
- Determine whether a variable is an array or an object?
- 金鱼哥RHCA回忆录:DO447管理项目和开展作业--创建作业模板并启动作业
- 指针和结构体
- Mathematical derivation from perceptron to feedforward neural network
- 连接集成开发专题月 | 企业主数据治理的驱动因素
- 什么是SSR/SSG/ISR?如何在AWS上托管它们?
猜你喜欢

UE4:Build Configuration和Config的解释

从感知机到前馈神经网络的数学推导

Online text batch inversion by line tool

Summary of submarine cable detection technology

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

Array exercises follow up

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

指针和结构体

Function key input experiment based on stm32f103zet6 Library

海底电缆探测技术总结
随机推荐
Data intelligence enters the "deep water area", and data governance is the key
流程判断-三目运算-for循环
ABAP随笔-面试回忆 望大家 需求不增 人天飙升
可观测,才可靠:云上自动化运维CloudOps系列沙龙 第一弹
One to one relationship
Online text batch inversion by line tool
Common shell script commands (III)
GIS remote sensing R language learning see here
Photoshop layer related concepts layercomp layers move rotate duplicate layer compound layer
Hanoi塔问题
Doctoral Dissertation of the University of Toronto - training efficiency and robustness in deep learning
Golang map 并发读写问题源码分析
刷题记录:Easy 数组(持续更新)
The Fifth Discipline: the art and practice of learning organization
UE4随笔:FString、FName 与 FText
UE4实现长按功能
Embracing cloud Nativity: Practice of Jiangsu Mobile order center
華大單片機KEIL報錯_WEAK的解决方案
Redis cluster Series II
指针和结构体