In a hash table, a value is stored by applying hash function on a key. Thus, values are not stored in a hash table in sorted order. Additionally, since hash tables use the key to find the index that will store the value, an insert/lookup can be done in amortised O(1) time (assuming only a few collisions in the hashtable). One must also handle potential collisions in a hashtable.
1) find min element
2) find max element
3) print elements in sorted order
4) find the exact element or, if the element is not found, find the next smallest number
1. A good hash function is required (e.g.: operation % prime number) to ensure that the hash values are uniformly distributed.
2. A collision resolving method is also needed: chaining (good for dense table entries), probing (good for sparse table entries), etc.
3. Implement methods to dynamically increase or decrease the hash table size on a given criterion. For example, when the [number of elements] by [table size] ratio is greater than the fixed threshold, increase the hash table size by creating a new hash table and transfer the entries from the old table to the new table by computing the index using new hash function.
You can use an STL map. Although this takes O(log n) time, since the number of inputs is small, this time is negligible.
本文探讨了哈希表与STL Map的区别:哈希表通过哈希函数存储值,提供平均O(1)的插入/查找操作;而STL Map按键排序存储,使用树结构实现O(log N)的时间复杂度。文章还讨论了哈希表的实现细节,如哈希函数的选择、碰撞解决策略等,并提出当输入较少时可以考虑使用STL Map。

被折叠的 条评论
为什么被折叠?



