I don't know if I'm overthinking this or this is just a really difficult problem or what.
(( "Suppose that a hash table contains hash_size = 13 entries indexed from 0 through 12 and that the following keys are to be mapped into the table:
10 100 32 45 58 126 3 29 200 400 0
Find a hash function that will produce no collisions for these keys. ))
I am struggling so hard with this because we already wrote a hashing program that had a separate function for resolving collisions. I think this problem is looking for a hash function that just has no collisions to start with and I can't find any guides on doing this.
The answer to the question is already available online:
1 2 3 4 5 6 7
constint hash_size = 13;
typedefint Key;
int hash(Key key)
{
int h = key/11 + key/13 + key/97 + 2003 − key + key/100 % 2 + key/10;
return h % hash_size;
}
I have no idea how they came up with this solution. I was trying to look up guides online but everyone just says to use a generator like gperf. Is there a method for figuring this out?
[ Edit: Most of the links (such as that to figure 1 are broken in a lot of the Dr Dobbs articles, but if you look towards the bottom of the page you'll see the article spans more than one page. figure 1 will be on page 2.) ]