English 中文(简体)
• 如何更好地执行我自己的地图
原标题:how can i improve the implementation of my own hash map

为学习目的,我撰写我自己执行散页地图。 http://en.wikipedia.org/wiki/Hash_table#Separate_chaining_with_list_heads” rel=“nofollow” 作为我的主题。

This is what the structure would look like:

| 0   | ---> | 11 | ---> | 33 | ---> | -- | ---> | 121 | ---> | TAIL |
| 1   | ---> | 12 | ---> | 34 | ---> | -- | ---> | 122 | ---> | TAIL |
| -   |
| -   |
| -   |
| D-1 | ---> | -- | ---> | -- | ---> | -- | ---> | -- | ---> | TAIL |

这是一系列相互关联的清单,

<>D>/strong> = 阵容大小,

缩略语

Algorithm:

void Insert(key, value):
 int bucket = hash_fn(key); // key % D, for now
 // accessing this bucket or array-index in array is O(1)
 // insert in linked list at the right position
 LL[bucket]->insert(new object(key, value))

bool Lookup(key):
 int bucket = hash_fn(key); // key % D, for now
 // search for key in LL[bucket]

<>Concern: 如果把许多要素绘制成同一个桶,则搜查不会是O(1),事实上,可能倾向于O(n)。

我如何改进?

最佳回答

你们可以 t。 因此,它必须利用良好的散射功能来防止这种情况发生,这种功能将物品平均扩散到桶内,并确保你使用足够的桶子。

If you re willing to stray from the idea of a hash table with linked lists for the buckets, you can try putting some other data structure in the buckets - for example some sort of self-balancing tree, like a red-black or AVL one to get O(log(m)) behaviour, where m is the maximum number of entries per bucket. But that will not actually make you happy. Simply using a good hash function will.

问题回答

http://en.wikipedia.org/wiki/Hash_table”rel=“nofollow”

有了良好的散列功能,平均看管费用几乎不变,因为装载系数从0升至0.7(约2/3升至2/3)。 除此以外,碰撞的概率和处理成本也有所增加。

因此,如果有足够的散射功能,而且有合理的厚颜色,你不应对此感到关切。

What you can do is Hashing with Chaining which will use a linked list to help avoid collisions within a hash table.
This will allow your lookups remain quite constant, even with many elements mapped to the same hash bucket.

然而,如果你期望散列桌接近能力,那么你就不需要担心此事。

This Wikipedia Article also contains some very good information about this technique.





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签