找到你要的答案

Q:Hash Table Size of Array

Q:数组的哈希表大小

Quick question why is it better to have a hash table size twice the size as the input array? Why would I want a table size of 40 (if my array was size 20) not something closer or even 20?

快速问题为什么有更好的哈希表大小的两倍大小作为输入数组?为什么我想要一个40的表大小(如果我的数组是大小为20)不是更接近,甚至20?

answer1: 回答1:

It depends on the collision method. If you have a perfect hash method then size 20 array is enough for 20 keys but in most cases there will be collisions and that means that they have to be placed at a new place in the array and finding that place can be difficult (sometimes impossible) if there is only one space left.

这取决于碰撞法。如果你有一个完美的散列方法,那么大小20阵列是足够的20键,但在大多数情况下,将有碰撞,这意味着他们必须放在一个新的地方,在阵列和查找的地方可能是困难的(有时是不可能的),如果只有一个空间。

answer2: 回答2:

You have to take precautions for collissions. Suppose the size is 20. And there are 20 values to place. If the hash function was not suitable for the data and all data was being directed to the last point that is 20, and there is no rollover measure in place, the data will be put in the next index i.e 21. If all the 20 data were to be mapped to this last index, you will need 20 more places. So for size of data n it is better to have size of hash table 2n

你必须采取预防措施,对碰撞过程。假设大小是20。并且有20个值要放置。如果散列函数是不适合的数据和所有的数据被引导到最后一点是20,并且没有翻转措施到位,数据将被放在下一个指标,即21。如果所有20个数据被映射到最后一个索引,您将需要另外20个地方。所以对于数据最好是有2n哈希表的大小

c++  hash  hashtable