哈希函数串 [英] hash function for string

查看:203
本文介绍了哈希函数串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的哈希表中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;
}

这篇关于哈希函数串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆