Hash (Hash) search algorithm detailed explanation of the C language version

1. Principle of Hash Search Algorithm

Hash search is a fast search algorithm. This algorithm does not need to compare keywords, but uses keywords as independent variables and the address of the keyword in the storage space as dependent variables to establish a certain functional relationship, called It is a hash function, so that when searching for a certain keyword, its address can be directly obtained through the hash function, which effectively improves the search efficiency.
The selection of the hash function and the basic principles mainly include: the time required to calculate the function, the length of the keyword, the length of the hash table (the range of the hash address), the distribution of the keyword, and the search frequency of the record.
There are many constructions of hash functions, such as "direct addressing method", "digital analysis method", "square method", "folding method", "remainder method", "random number method" and so on.
A basic principle of hash function construction is to avoid conflicts as much as possible, that is, to avoid conflicts of dependent variable addresses as much as possible. Once a conflict occurs, re-addressing is required. Common methods to deal with address conflicts include: "open addressing method", "re-hashing method", "chain address method", "establishing a public overflow area" and so on.

2. Example of creating hash lookup and hash interpolation table

Suppose there is data { 10, 8, 14, 15, 20, 31 }, create a hash table for hash lookup.
1. Create a hash table
Construct a hash function with the remainder method, use the linear detection method as a method to deal with conflicts, take the length of the hash table as 7, and build the hash table as follows: H(10) = 10 % 7
= 3
H(8) = 8 % 7 = 1
H(14) = 14 % 7 = 0
H(15) = 15 % 7 = 1 Conflict at this time, re-address: H(15) = (H(15)+1 ) % 7 = 2
H(20) = 20 % 7 = 6
H(31) = 31 % 7 = 3 At this time conflict, re-address: H(31) = (H(31)+1) % 7 = 4
ha The table is as follows:
insert image description here
2. Hash search
When looking for an element, first calculate its hash address through the hash function, and then compare whether the value of the address is equal to the target value, if they are equal, the search ends, otherwise use the conflict handling method to determine the new address, and then compare. If the hash address is empty, the lookup fails.
Using the hash function to calculate the address of element 15 is 1. At this time, the element in the table is not equal to 15. Therefore, the linear detection method is used to update the hash address, and the new address is 2. At this time, the search is successful.

3. C program of hash lookup algorithm

Using the "remainder method" as the hash function and the "linear detection method" as the method to deal with conflicts, give the C program for creating the hash table and the hash search algorithm.
1. The structure of the hash table:
the members in the member list can be adjusted according to actual needs.

typedef struct HashTable
{
    
    
	int key;      //关键字 
	int EmptyFlag;//占用(冲突)标志,0表示没被占用,1表示被占用 
}HashTable;

2. Create a hash table

//tbl:哈希表
//data:已知的数组
//m:数组的长度
//p:哈希表的长度 
void CreateHashTable( HashTable *tbl, int *data, int m, int p )
{
    
    
	int i, addr, k;
	for( i=0; i<p; i++ ) //把哈希表被占用标志置为0 
	{
    
    
		tbl[i].EmptyFlag = 0;
	}
	for( i=0; i<m; i++ )
	{
    
    
		addr = data[i] % p;//计算哈希地址 
		k = 0;//记录冲突次数 
		while( k++ < p )
		{
    
    
			if( tbl[addr].EmptyFlag == 0 )
			{
    
    
				tbl[addr].EmptyFlag = 1;//表示该位置已经被占用 
				tbl[addr].key   = data[i];
				break;
			}
			else
			{
    
    
				addr =  ( addr + 1 ) % p; //处理冲突 
			}
		}	
	}
}

3. Hash Lookup

int SearchHashTable( HashTable *tbl, int key, int p )
{
    
    
	int addr, k, loc;//loc表示查找位置下标,如果为0则表示查找失败 
	addr = key % P;//计算Hash地址 
	loc = -1; 
	k = 0;//记录冲突次数 
	while( k++ < p )
	{
    
    
		if( tbl[addr].key == key )
		{
    
    
			loc = addr;
			break;
		}
		else
		{
    
    
			addr =  ( addr + 1 ) % p; //处理冲突 
		}	
	}
	return loc;
}

3. Completed test code

#include"stdio.h"
#define M 6
#define P (M+1)
typedef struct HashTable
{
    
    
	int key;      //关键字 
	int EmptyFlag;//占用(冲突)标志,0表示没被占用,1表示被占用 
}HashTable;

void CreateHashTable( HashTable *tbl, int *data, int m, int p );
int SearchHashTable( HashTable *tbl, int key, int p );

int main()
{
    
    
	HashTable HashTbl[P];
	int data[M] = {
    
     10, 8, 14, 15, 20, 31 };
	int i, loc;
	printf( "初始数据:\n" );
	for( i=0; i<M; i++ )
	{
    
    
		printf( "data[%d] = %5d\n", i, data[i] );
	}
	printf( "\n" );
	CreateHashTable( HashTbl, data, M, P );
	printf( "哈希表:  \n" );
	for( i=0; i<M; i++ )
	{
    
    
		printf( "tbl[%d] = %5d\n", i, HashTbl[i].key );
	}
	printf( "\n" );
	for( i=0; i<M; i++ )
	{
    
    
		loc = SearchHashTable( HashTbl, data[i], P );
		printf( "%5d 's loc = %5d\n", data[i], loc );
	}
	
	return 0;
}
void CreateHashTable( HashTable *tbl, int *data, int m, int p )
{
    
    
	int i, addr, k;
	for( i=0; i<p; i++ ) //把哈希表被占用标志置为0 
	{
    
    
		tbl[i].EmptyFlag = 0;
	}
	for( i=0; i<m; i++ )
	{
    
    
		addr = data[i] % p;//计算哈希地址 
		k = 0;//记录冲突次数 
		while( k++ < p )
		{
    
    
			if( tbl[addr].EmptyFlag == 0 )
			{
    
    
				tbl[addr].EmptyFlag = 1;//表示该位置已经被占用 
				tbl[addr].key   = data[i];
				break;
			}
			else
			{
    
    
				addr =  ( addr + 1 ) % p; //处理冲突 
			}
		}	
	}
}

int SearchHashTable( HashTable *tbl, int key, int p )
{
    
    
	int addr, k, loc;//loc表示查找位置下标,如果为0则表示查找失败 
	addr = key % P;//计算Hash地址 
	loc = -1; 
	k = 0;//记录冲突次数 
	while( k++ < p )
	{
    
    
		if( tbl[addr].key == key )
		{
    
    
			loc = addr;
			break;
		}
		else
		{
    
    
			addr =  ( addr + 1 ) % p; //处理冲突 
		}	
	}
	return loc;
}

4. Test results
insert image description here

Guess you like

Origin blog.csdn.net/sunnyoldman001/article/details/127345993