【发布时间】:2016-03-11 10:45:46
【问题描述】:
我已经实现了二进制搜索、线性搜索和哈希表来比较每种时间复杂度。问题是,当我测量时间以查找素数时,我的哈希表比二进制搜索慢得多。以下是我的代码:
// Make the hash table 20 times the number of prime numbers
HashTable::HashTable(std::vector<int> primes)
{
int tablesize = primes.size() * 20;
table = new std::list<int>[tablesize];
size = tablesize;
for (auto &prime : primes)
this->insert(prime);
}
// Hash function
int HashTable::hash(int key)
{
return key % size;
}
// Finds element
int HashTable::find(int key)
{
// Get index from hash
int index = hash(key);
// Find element
std::list<int>::iterator foundelement = std::find(table[index].begin(), table[index].end(), key);
// If element has been found return index
// If not, return -1
if (foundelement != table[index].end())
return index;
else
return -1;
}
// Adds element to hashtable
void HashTable::insert(int element)
{
// Get index from hash and insert the element
int index = hash(element);
table[index].push_back(element);
}
哈希表.h
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <list>
#include <iostream>
#include <vector>
class HashTable
{
private:
// Each position in Hashtable has an array of lists to store elements in case of collision
std::list<int>* table;
// Size of hashtable
int size;
// Hashfunction that returns the array location for the given key
int hash(int key);
public:
HashTable(int tablesize);
HashTable(std::vector<int> primes);
// Adds element to hashtable
void insert(int element);
// Deletes an element by key
void remove(int key);
// Returns an element from hashtable for a given key
int find(int key);
// Displays the hashtable
void printTable();
// Display histogram to illustrate elements distribution
void printHistogram();
// Returns the number of lists in hash table
int getSize();
// Returns the total number of elements in hash table
int getNumberOfItems();
// De-allocates all memory used for the Hash Table.
~HashTable();
};
#endif
我已经尝试超过表格大小以消除冲突,但我没有发现任何差异。
【问题讨论】:
-
这是一个非常好的图表。看起来像人们期望的那样:哈希搜索具有恒定的时间复杂度,而二进制具有对数的时间复杂度。只是哈希表的常数相当大。向量与缓存配合得很好。
-
如果您将
table的类型更改为std::vector<int> *,您的计时会发生什么变化? -
如果您将
key % size更改为key % 12345,我的意思是,硬编码计数?会更快吗?我认为,除法可能有点太慢了。 (顺便说一句,这通常是一种不好的哈希函数,除非除数是素数)。另外,您编译代码时是否开启或关闭优化? -
int tablesize = primes.size() * 20;- 这是很多浪费的空间(以及,因此,时间) -
我相信@msandiford 的建议是使用向量数组而不是列表数组。您对数组中的元素所做的只是搜索和追加;
vector的表现应该优于list。
标签: c++ hashtable binary-search-tree