简体   繁体   中英

Array size of hashtable?

I'm reading a book about Algorithm to find a better alternative to list. The book mentions that the array size for hash table has to be twice the size of the items that need to be added. However, no mention of the array size when it discusses separate chaining. Does the array size still has to be twice the items to be added? Can it be the same size as the items since each index can contain more than one item? And will that affect the performance?

This is depends for your memory/speed tradeoff requirement. For chaining schemes, I recommend use hashtable size as 1/2..1/4 of keys quantity. This is ~1-2 comparisons per lookup, if you keep each linklist sorted by keys.

Also, for increase performance, you can use "barrier element". This is special node, contains "barrier value, greater than all possible keys", and last element of all liknkist referred not to the NULL, but to that barrier node instead.

By this way, you do not need compare pointer to "is linklist ends", you need just compare keys only like (lists are sorted):

for(node *p = table[hash]; p->key < search_key; p = p->next);
return p->key == search_key? p : NULL;

In case of chaining the hash table size does not matter that much as compared to probing but the hash function that maps your keys to buckets matter a lot because if hash function doesnt evenly distribute the keys in the table then you might end up doing linear search for an element. Any table size of O(N) is recommended like (1,1/2,1/4..) to get O(1) search and insert provided hash function must evenly distribute the data in the table.

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