hash algorithm

Dave Decot decot at hpisod2.HP.COM
Wed Aug 17 19:13:13 AEST 1988


> could anyone recommend a hashing algorithm to store a list of 10
> digit phone numbers (area code plus 7 digits).
> It should have little or no overflow and no collisions (as few as
> possible). The list will contain more than one area code but
> about 1000 numbers per area code (ie not a totasly random sample
> but one which has a relatively common prefix)
> thank you very much.

Treat the ten digits as an integer, n, and compute the index
as (n % HASH_SIZE), where HASH_SIZE is the size of your hash table.

Store the number, and the record for that number, in the first empty
hash table location at or after the index you've computed.

HASH_SIZE should be about four times the maximum number of phone numbers
you expect to store, to reduce collisions.

What data is associated with each phone number?  What do you intend
to do with the list?  Do you want to look up things by phone number,
or by some other key?

Dave



More information about the Comp.lang.c mailing list