Lab#06 Hashing
Lab#06 Hashing
LAB # 06
Hashing techniques
Objective
The purpose of this lab is to implement Hashing techniques with collision resolution.
Theory
Hashing technique is used for ordering and accessing elements in a list in a relatively
constant amount of time by manipulating the key to identify its location in the list.
Functions use to range a key values which is transformed into a range of array index
values is called hash function and the particular index value is called key in a hash
table.
Different types of hash functions are used for the mapping of keys into tables.
a) Division Method
b) Mid-square Method
c) Folding Method
A hash table is a data structure that offers very fast insertion and searching. No matter
how many data items there are, insertion and searching (and sometimes deletion) can
take close to constant time.
Collision
The condition when resulting two or more keys produce the same hash location.
A good hash function minimizes collision by spreading the elements uniformly
throughout the array. We can minimize collision but it’s extremely difficult to avoid them
completely. There are many collision resolution algorithm.
a) Linear Probing
b) Quadratic Probing
c) Double Hashing.
Linear Probing
A simple approach to resolving collisions is to store the colliding element in the next
available space.
Insert Algorithm
// end of loop
Search Algorithm
// is correct hashVal?
//end of loop
Delete Algorithm
1. loc:= find(key)
2. If loc=-1 then return null
3. Else
4. DataItem temp = hashArray[loc]; // save item
5. hashArray[loc] = nonItem; // delete item
6. return temp; // return item
//end of loop
// end delete()
Linear Probing
linear_probing_insert(K)
if (table is full) error
probe = h(K)
while (table[probe] occupied)
probe = (probe + 1) mod M
table[probe] = K
Lab Task
1. Write a program which shows the insertion of the elements in a hashtable and
then search for a given
Home Task