cstutoringcenter.com logo
E-mail:Password:

Become a premier member today to gain access to exclusive
member benefits! Just $5.00 to join FOR LIFE!






Hashing data is a form of organizing and sorting data to fit your needs. There are 2 parts to any hashing: Smokey is the hash function; Buckets are the place holders for the data.

Hashing

In general, a hash table is a way to store data and it is also good for searching data.

An "ideal" has function: If there are B buckets and N items to put in them, it should be closed to N/B items.

A "bad" has function: If there are B buckets and N items to put in them, the most items are assigned to very few buckets.

Open Hashing

Hashing looks very similar to the radix sort we have seen before. Below is how a hash table may look:

Index	Data
0	X -> X -> ///
1	X -> X -> X -> ///
2	X -> ///
3	X -> X -> ///
.
.
.
B-1	X -> ///

where B is the number of buckets you have 
and X is any piece of data you hashed.

Real Hashing

Consider the following:

U = universal set
S = subset of U
x = a member of S

Therefore:

Hash(x) = Mod(x, b)

where x is in S and b is the number of buckets you have.

Let's see an example of this using a trivial case:

U = {1,2,3,4,5, ...}		the natural numbers
S = {2,3,5,7,11,13,17,19}	a set of primes

Evaluating each of the Hash functions below:

Hash(2) = Mod(2, 8) = 2
Hash(13) = Mod(13, 8) = 5
Hash(19) = Mod(19,8) = 3

EtcÂ…

Another example may be the following:

U = {1,2,3,4,5, ...}		the natural numbers
S = {x | 0 < x < 9999}	numbers from 1 to 9998

Using the following definition of a Hash function:

Hash(x) = Mod(sumdigits(x), 19)

Evaluate each of these:

Hash(234) = Mod(9, 19) = 9
Hash(123456) = Mod(21, 19) = 2
Hash(8) = Mod(8, 19) = 8
Hash(199) = Mod(19, 19) = 0

To put it simply, hashing keeps the data organized by making use of the mod function.