哈希算法简介

来源:互联网 发布:淘宝卖家中心进不去 编辑:程序博客网 时间: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 1for short,temp equals to 40503  2for int ,temp equals to 2654435769  3for 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;        }    }}