I know something like this has been asked before, but the answer was sort of side tracked.
I want to develop a hash function which will take a word and spit out an address of an array.
So, for example, if you input god:
sort the word, d o g
perform some sort of function on this to get an address d o g -> some number
insert 'dog' into address some_number in array[].
I can't seem to make a function which doesn't get screwed up somehow.
public static int hashCode(String word){
char[] x = word.toCharArray();
Arrays.sort(x);
int hash = 0;
for(int i =0; i<x.length; i++)
{
hash +=(x[i]-96)*(x[i]-96)*(x[i]-96)*(i+1)*(i+1)+i;
}
hash %=size; // get a value that's inside the bounds of the array
if(hash<0)
hash = hash + size;
return (hash);
}
This is my current algorithm but there are two problems.
the array size has the be huge so that there aren't a ton of collisions
there still are a few collisions, chair for example, produces: smarminess, parr, chair
What do you guys think? I really appreciate your help