Hash search of algorithm

[algorithm interpretation]: hash lookup is also called hash table

[introduction question]: suppose a grocery store has customers coming to buy things. You have to find the price from the book. But with the increase of commodities, using dichotomy can't make customers want to ask the price of any commodity, they should answer immediately. You need to use a hash table.

[conflict resolution]: hash is a storage method that can be accessed quickly. Generally, the data item as the retrieval part is an integer or a string. When it is a string, the number of strings is far greater than the length of the array. In this case, there will be multiple strings mapped to a storage location. This is the so-called conflict problem, which must exist when there is a conflict. At this time, how to realize data storage needs to be solved.

[program design]:

/* 	hash algorithm */
#define HASHSIZE 7
#define NULLKEY -32768

typedef int Status;
typedef struct
{
	int *elem;	/*	Base address */
	int count;	/*	Number of current data elements */
}HashTable;

uint8_t m = 0;  /* Hash table length */

/*	Initialization */
Status Init(HashTable *hashTable)
{
	uint8_t i;
	m = HASHSIZE;
	hashTable->elem = (uint8_t *)malloc(m * sizeof(int));
	hashTable->count = m;
	for( i = 0; i < m; i++ )
	{
		hashTable->elem[i] = NULLKEY;
	}
	return TRUE;
}

/*	Hash function (division residual method) */
uint8_t Hash(uint8_t data)
{
	return data % m;
}

/*	insert */
void Insert(HashTable *hashTable,uint8_t data)
{
	uint8_t hashAddress = Hash(data); /*	Find hash address */

	/*	Clashes */
	while(hashTable->elem[hashAddress] != NULLKEY)
	{
		/* 	Using linear detection of open address to resolve conflicts */
		hashAddress++;
		hashAddress = (hashAddress) % m;
	}

	/*	insert values*/
	hashTable->elem[hashAddress]= data;
}

/*	lookup */
uint8_t Search(HashTable *hashTable,uint8_t data)
{
	uint8_t hashAddress = Hash(data); /*	Find hash address */

	/*	Clashes */
	while(hashTable->elem[hashAddress] != data)
	{
		/* 	Using linear detection of open address to resolve conflicts */
		hashAddress++;
		hashAddress = (hashAddress) % m;

		if(hashTable->elem[hashAddress] == NULLKEY || hashAddress == Hash(data))
		{
			return -1;
		}
	}

	/*	Search success */
	return hashAddress;
}

/*	Print results */
void Display(HashTable *hashTable)
{
	uint8_t i;
	for ( i = 0; i < hashTable->count; i++)
	{
		printf("%5d",hashTable->elem[i]);
	}
	printf("\n");
}

[call example]:

int main()
{
	uint8_t i;
	uint8_t result;
	HashTable hashTable;
	uint8_t arr[HASHSIZE] = {13,29,27,28,26,30,38};

	/*	Initialize hash*/
	Init(&hashTable);

	printf("	4.Hash lookup algorithm:\n");

	/*	insert data*/
	for( i = 0; i < HASHSIZE; i++)
	{
		Insert(&hashTable,arr[i]);
	}
	Display(&hashTable);

	/*	Find data*/
	result = Search(&hashTable,29);
	if(result == -1)
	{
		printf("error\n");
	}
	else
	{
		printf("data is %d",result);
		printf("\n");
	}
        return 0;
}

 

Posted by countcet on Sat, 26 Oct 2019 12:39:36 -0700