哈希函数串函数

2023-09-10 22:34:00 作者:回眸一笑抹倾城

我工作的哈希表中C语言和我测试哈希函数的字符串。

I'm working on hash table in C language and I'm testing hash function for string.

第一个功能我已经试过是添加ASCII code和使用模(100%),但我已经得到了数据的第一个测试效果差:40碰撞130字。

The first function I've tried is to add ascii code and use modulo (%100) but i've got poor results with the first test of data: 40 collisions for 130 words.

最后输入数据将包含8万字(这是一个dictionnary存储在一个文件中)。哈希表被声明为INT表[10000]及包含单词的txt文件的位置。

The final input data will contain 8 000 words (it's a dictionnary stores in a file). The hash table is declared as int table[10000] and contains the position of the word in a txt file.

的第一个问题是它是用在哈希串最好的算法?以及如何确定的哈希表的大小?

The first question is which is the best algorithm for hashing string ? and how to determinate the size of hash table ?

在此先感谢!

: - )

推荐答案

我已经受够了不错的成绩 < $ C C> djb2 $由丹·伯恩斯坦。

I've had nice results with djb2 by Dan Bernstein.

unsigned long
hash(unsigned char *str)
{
    unsigned long hash = 5381;
    int c;

    while (c = *str++)
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

    return hash;
}