stper** pages;
int tableSize;
struct Person{
string name;
int age;
string homeTown;
};
void fonk1 (int numberOfBuckets)
{
pages = new stper*[numberOfBuckets]();
tableSize = numberOfBuckets;
}
int hashPerson(Person& person)
{
int hashVal = 0;
for (int i=0; i < (person.getName()).length() ; i++)
hashVal = 37*hashVal + (person.getName())[i];
for (int i=0; i < (person.getHomeTown()).length() ; i++)
hashVal = 37*hashVal + (person.getHomeTown())[i];
hashVal+= person.getAge();
hashVal %= tableSize;
if(hashVal < 0)
hashVal += tableSize;
return hashVal;
}
Hello everyone, I am new at hashing. My hashing function is at above in hashPerson function and asyou can see there are three key. Is my function a good algorithm for hashing and how can I improve the function and decrease the number of collision? (Please ignore if there are any syntax mistakes)
I have a few suggestions:
Use
unsignedinstead ofint. In my experience this has proven to perform better, as when the unsigned overflows, it still stays non-negative(otherwise %-ing may lead to big problems – you get a negative index and … a crash) and it also results in reduced collision rate(empirically proven). Also after all the function is supposed to return an index in a table so it is natural for the value to be unsigned – the index can not be negative.Multiply hashVal by something when adding the age. I would suggest a value larger then any possible age for instance 200.
You never say what is
tableSizebut I would advice you to use some big(as big as possible) prime number, again to reduce collision rate.