We are currently dealing with hash function in my class. Our instructor asked us to a hash function on the internet to compare to the two we have used in our code.
The first one:
int HashTable::hash (string word)
// POST: the index of entry is returned
{ int sum = 0;
for (int k = 0; k < word.length(); k++)
sum = sum + int(word[k]);
return sum % SIZE;
}
Second:
int HashTable::hash (string word)
{
int seed = 131;
unsigned long hash = 0;
for(int i = 0; i < word.length(); i++)
{
hash = (hash * seed) + word[i];
}
return hash % SIZE;
}
Where SIZE is 501 (The size of the hash table) and the input is coming from a text file of 20,000+ words.
I saw this question with a few code examples but wasn't exactly sure what to be looking for in a hash function. If I understand correctly, in my case, a hash takes an input (string) and does a math calculation to assign the string a number and inserts it in a table. This process is done to increase the speed of searching the list?
If my logic is sound, does anyone have a good example or a resource showing a different hash function that involves a string? Or even the process of writing my own efficient hash function.