哈希算法简介
来源:互联网 发布:淘宝卖家中心进不去 编辑:程序博客网 时间:2024/06/05 11:04
introduction to HashTable
1.What is hash?
HashTable is a kind of map. The search process in hashtable holds o(1) complexity. You may think o(1), that’s impossible, since you need to traversal all the container to find the object that you want. However, in hashtable, it’s unnecessary to do so, because you can find the object just using a simple map. Concretely, hash table is an array, you can use index to get the object you want. And index in array holds a complexity of o(1).
2.How to build a hashtable
If we have an enough big array, to load all the statistic you store, then you can easily get the thing you want with an index. Actually, it’s unreal to achieve this kind of hash for when you have a large large number of objects, which are not consistent, then the space cost is quite high. Thus, we need to find a new way to build a hashtable in limited space. But, how can we do that? There are several methods:
- %m with an array(size m)
- fibonacci method
index = (value * temp) >> 28 1,for short,temp equals to 40503 2,for int ,temp equals to 2654435769 3,for long long,temp equals to 11400714819323198485
As we know that, this kind of map will probably cause crash between or among some maps(say, is not a onto map). Hence, we need to find a way to handle the problems when different objects map to the same index. And the most common way to solve this problem is using linklist. Every hashtable’s entry points to a linklist. Then, if an object get an index same as the other, just push this object to the end of the linklist. When we need to find, just to traverse this linklist to determine whether this object exists.
3.This is a simple hashtable
to find if a string has appeared in the hash table before
code:
#include <iostream>#include <vector>//const unsigned long HashValue=11400714819323198485;using namespace std;typedef struct Node{ string name; struct Node* next;}node;vector<string>input;node* hashTable[1000];unsigned long HashString(const char *lpszString){ unsigned long ulHash = 0xf1e2d3c4; while (*lpszString != 0) { ulHash <<= 1; ulHash += *lpszString++; } return ulHash;}void IntoHash(const string& string1, unsigned long pos){ if(!hashTable[pos]){ auto ptr=new node; ptr->name=string1; ptr->next=NULL; hashTable[pos]=ptr; } else{ auto temp=hashTable[pos]; while(temp->next!=NULL){ temp=temp->next; } auto ptr=new node; ptr->name=string1; ptr->next=NULL; temp->next=ptr; } return;}bool Search(const string& string1){ auto index=HashString(string1.c_str()); if(hashTable[index%1000]==NULL){ return 0; } else{ auto ptr=hashTable[index%1000]; do{ if(ptr->name==string1){ return 1; } else{ ptr=ptr->next; } }while(ptr!=NULL); } return 0;}int main(void){ memset(hashTable,NULL,1000); int n; cin>>n; string temp; for(int i=0;i<n;i++){ cin>>temp; input.push_back(temp); } for(const auto i:input){ unsigned long pos=HashString(i.c_str()); auto index=pos%1000; //unsigned long index = (pos * HashValue) >> 28; IntoHash(i,index); } int m; cin>>m; for(int i=0;i<m;i++){ cin>>temp; if(Search(temp)){ cout<<"find"<<endl; } else{ cout<<"opps,not exists"<<endl; } }}
- 哈希算法简介
- 哈希算法简介
- 哈希分布与一致性哈希算法简介
- 哈希分布与一致性哈希算法简介
- 哈希分布与一致性哈希算法简介
- 哈希分布与一致性哈希算法简介
- 哈希分布与一致性哈希算法简介
- 哈希分布与一致性哈希算法简介(1)
- 哈希分布与一致性哈希算法简介
- [转]哈希分布与一致性哈希算法简介
- 哈希分布与一致性哈希算法简介
- 哈希分布与一致性哈希算法简介
- 哈希分布与一致性哈希算法简介
- 哈希分布与一致性哈希算法简介
- 分布式服务器中一致性哈希算法的简介
- 算法简介
- 算法简介
- 算法简介
- 查找数据库里的blob字段并保存在文件中
- Emoji表情在web端的展示步骤详解!!!
- JavaScript-DOM(document object model)
- Redis 3.0集群原理
- android ipc跨进程通许
- 哈希算法简介
- 在腾讯云中配置ubuntu系统 VPN
- Be the Winner HDU-2509【Nim经典博弈】
- Xdebug安装配置
- 20170803工作记账流水(2017-08-03 10:00)
- H5 学习之 拖动事件
- 【CSS 基础】10 float
- Sublime Text 3如何快速生成HTML5的头部信息和常用的快捷键
- 【DL--15】运行一个TensorFlow