An instance of a hash table lookup

Source: Internet
Author: User

The workaround here is to open the address law: "Open address refers to the table is not yet occupied address, open address law is when the conflict occurs, the formation of an address sequence, along the sequence of detection, until an empty open address, the occurrence of the conflict of the keyword stored in the address, namely hi= (H ( Key) +di)%m,i=1,2,.. K (k<=m), where H (key) is a hash function, M is the hash table length, and di is the increment sequence.

Example: Choosing the hash function H (k) = (3K)%11, dealing with conflicts with open addresses, D1=h (k);d i= (di+ (7K)%10+1)%11 (i=2,3,..), trying to,.. in ht[0 10] The hash address space of the keyword {22,41,53,46,30,13,1,67} Constructs a hash list, and finds the average length of the success under equal probability, and designs a complete algorithm program to construct the hash list.

Test data: {22,41,53,46,30,13,1,67}

Output: i=0 i=1 i=2 i=3 1 i=4 i=5 67 i=6

Cs=18 asl=2.25

1 algorithm idea: the construction of the hash table is based on the hash list function Kernel processing conflict, the different key records stored to different hash list address process, so the main operation is to use the hash function to find the hash address, if the conflict resolution conflict, ensure that the records of different keywords stored to a different hash address.

2. Data structure and hash function and conflict resolution selection: Take hash function H (k) = (3K)%11, with open address conflict, d1=h (K);d i= (di+ (7K)%10+1)%11 (i=2,3,..)

3. Module partitioning (1) initialization of the hash space array Inithash; (2) hash function hash (3) Insert function Inserthash (4) Find function Searchhash (5) Main function

1#include <stdio.h>2#include <stdlib.h>3 #defineN 84 #defineM 115 voidInithash (intht[])6 {7 /*Initializes a hash array of stored records with integer 0*/8 inti;9  for(i=0; i<m;i++)Tenht[i]=0; One } A intHash (intkey) - { -     /*hash Function*/ the     return(3*key)%m; - } - voidInserthash (intHt[],intkey) - { +     /*Insert function to insert key into the hash table HT*/ -     intd,s1,d1,h,temp; +H=hash (key);/*Call hash function to calculate hash address*/ AS1=d1=h;/*Preserve hashed addresses for use when resolving conflicts*/ at     if(Ht[h])/*Determine if conflicts, if conflicts, resolve conflicts with open address law for the first time*/ -H= (D1+S1)%m; -D=D1; -      while(Ht[h]) -     { -         /*resolve the second and subsequent conflicts*/ intemp=D; -D= (temp+ (7*key)%Ten+1)%m;/*calculate di (i=2,3.)*/ toH= (s1+d)%m; +     } -ht[h]=key; the  * } $ intSearchhash (intHt[],intkey)Panax Notoginseng { -     /*find the key in the hash list, return the comparison number if found, otherwise return-1*/ the     intd,s1,d1,h,temp,sum; +H=Hash (key); AS1=h;d1=s1;d=D1; the     if(Ht[h]==key)return 1; +     Else{ -H= (D1+S1)%m; $         if(Ht[h]==key)return 2; $sum=1; -         while(Ht[h]) -        { the            if(Ht[h]==key)return++sum; -            Else{Wuyisum++;temp=D; theD= (temp+ (7*key)%Ten+1)%m; -H= (s1+d)%m; Wu            } -        } About     } $     return-1; - } - intMain () - { A    //printf ("Hello world!\n"); +    /*outputs the position of each record in the hash table and calculates the total number of lookups and average lookup lengths*/ the    intx[n]={ A, A, -, $, -, -,1, the}; -    intht[m],sum,i; $    DoubleCS; the Inithash (HT); the     for(i=0; i<n;i++) the Inserthash (Ht,x[i]); the     for(i=0; i<m;i++) -     if(ht[i]!=0) inprintf"i=%d%d", I,ht[i]); the     theprintf"\ n"); Aboutcs=0; the      for(i=0; i<n;i++){ thesum=Searchhash (Ht,x[i]); the         if(sum!=-1) +cs+=sum; -     } theprintf"cs=%f\n", CS);Bayiprintf"asl=%f\n", (CS)/n); the     return 0; the}

Reading notes = =

An instance of a hash table lookup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.