简体   繁体   中英

How does a hash index relate to the array size?

I'm making a program to read the strings from a file into a hash table using separate chaining and I want to use the djb2 hash algorithm. When, for example, I hash the word "welcome" I get a hash index of 7573091155873627. Does this mean that the array that holds my hash table needs to be this gargantuan? I only really expect to read about 100 words or so. I just want to be sure that I can have my hash table set to hold 100 items and still use this algorithm.

When you put an entry into the array of a hash table, the bucket you choose is

hashvalue modulo size of the array 

Si there is no problem having extremely large hash value. On the contrary, they allow you to use arbitrary large arrays, which allows you to hash any number of items. Actually, in standard implementation, the size of the array grows when the hash array becomes too full.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM