You are on page 1of 10

EXPERIMENT NO-5: HASHING

Hashing is a technique or process of mapping keys, and values into the hash table by using a
hash function. It is done for faster access to elements. The efficiency of mapping depends on
the efficiency of the hash function used.
Hashing is useful for several reasons. Firstly, it can reduce the amount of memory required
to store large data sets by converting the data into a smaller value. Secondly, it can improve
the performance of algorithms by allowing for faster searching and retrieval of data. Finally,
it can help to ensure data integrity by detecting duplicate data and preventing collisions
(when two different keys map to the same index).

The process of hashing involves three main steps: creating the hash function, generating the
hash value, and storing the data in the hash table.

Hash Table
The Hash table data structure stores elements in key-value pairs where

 Key- unique integer that is used for indexing the values


 Value - data that are associated with keys.

Key and Value in Hash table


Hashing (Hash Function)
In a hash table, a new index is processed using the keys. And, the element corresponding to
that key is stored in the index. This process is called hashing.
Let k be a key and h(x) be a hash function.
Here, h(k) will give us a new index to store the element linked with k.
Hash Collision
When the hash function generates the same index for multiple keys, there will be a conflict
(what value to be stored in that index). This is called a hash collision.
We can resolve the hash collision using one of the following techniques.

 Collision resolution by chaining

 Open Addressing: Linear/Quadratic Probing and Double Hashing

1. Collision resolution by chaining


 In chaining, if a hash function produces the same index for multiple elements, these
elements are stored in the same index by using a doubly-linked list.
 If j is the slot for multiple elements, it contains a pointer to the head of the list of
elements. If no element is present, j contains NIL.

Collision Resolution using chaining


2. Open Addressing
Unlike chaining, open addressing doesn't store multiple elements into the same slot. Here,
each slot is either filled with a single key or left NIL.
Different techniques used in open addressing are:

i. Linear Probing

In linear probing, collision is resolved by checking the next slot.

h(k, i) = (h′(k) + i) mod m


where

 i = {0, 1, ….}
 h'(k) is a new hash function
If a collision occurs at h(k, 0), then h(k, 1) is checked. In this way, the value of i is
incremented linearly.
The problem with linear probing is that a cluster of adjacent slots is filled. When inserting a
new element, the entire cluster must be traversed. This adds to the time required to perform
operations on the hash table.

ii. Quadratic Probing

It works similar to linear probing but the spacing between the slots is increased (greater than
one) by using the following relation.

h(k, i) = (h′(k) + c1i + c2i2) mod m


where,

 c1 and c2 are positive auxiliary constants,


 i = {0, 1, ….}
iii. Double hashing

If a collision occurs after applying a hash function h(k), then another hash function is
calculated for finding the next slot.
h(k, i) = (h1(k) + ih2(k)) mod m
IMPLEMENTATION: -

#include<stdio.h>
#definesize 7
intarray[size];

void init()
{inti;
for(i=0;i<size; i+
+)array[i]=-1;
}

voidinsert(intval){
intkey=val%size;

if(array[key]==-1)
{array[key]=val;
printf("%dinsertedatarray[%d]\n",val,key);
}else{
printf("Collision:array[%d]haselement%dalready!\
n",key,array[key]);printf("Unabletoinsert%d\n", val);
}
}

voiddel(intval){
int key = val
%size;if(array[key]
==val)
array[key] =-
1;else
printf("%dnotpresentinthehashtable\n",val);

void search(int val)


{intkey=val%size;
if(array[key]==val)
printf("Search Found\
n");else
printf("SearchNotFound\n");

}
void print()
{inti;
for(i=0;i<size;i++)
printf("array[%d]=%d\n",i,array[i]);
}

int main()
{init();

while (1) {
printf("Chooseanoption:\
n");printf("1.Insertvalue\n");
printf("2. Delete value\
n");printf("3.Searchvalue\
n");
printf("4.Printhashtable\
n");printf("5.Exit\n");

int choice,
value;scanf("%d",&c
hoice);

switch(choice){

case1:
printf("Entervaluetoinsert:");sc
anf("%d",&value);
insert(value);
break;

case2:

printf("Entervaluetodelete:");sc
anf("%d",&value);
del(value);
break;
case3:

printf("Entervaluetosearch:");sc
anf("%d",&value);
search(value);
break;

case4:

printf("Hash table\
n");print();
printf("\
n");break;
case5:
return
0;default:
printf("Invalidchoice.Pleasetryagain.\n");

return0;

}
OUTPUT: -
Applications of Hash Table
1. Data Retrieval and Storage: Hash tables are primarily used to store key-value pairs. They
provide constant-time average complexity for insertions, deletions, and lookups, making
them efficient for tasks like caching, database indexing, and storing configuration data.

2. Databases: Hash tables are used in database management systems to index and retrieve
data efficiently. They can help improve the performance of data retrieval operations by
quickly locating records associated with a specific key.

3. Caching: Caching frequently accessed data can improve the performance of an


application. Hash tables are often used to implement caches, where the keys are the items to
be cached, and the values are the cached data.

4. Symbol Tables: Compilers and interpreters often use hash tables to store information
about variables, functions, and classes. These tables allow the quick lookup of symbols
during parsing and interpretation.

5. Password Storage: Hash tables are used to securely store passwords by hashing and
salting them. The hash of a password is stored, and during authentication, the entered
password is hashed and compared to the stored hash for verification.

6. Frequency Counting: Hash tables can count the frequency of elements in a dataset,
making them useful for tasks like finding the most common words in a text or identifying
popular items in a list.

7. Memorization: In dynamic programming and recursive algorithms, hash tables can be


used to cache intermediate results, reducing redundant computations and improving
performance.

8. Associative Arrays: Hash tables are often used to implement associative arrays or
dictionaries, where keys map to values. This is a common way to store and access
configuration settings or data structures in many programming languages.

9. Blockchain: In cryptocurrencies like Bitcoin, hash functions are used to maintain the
blockchain's integrity and to create a secure ledger of transactions.
CONCLUSION:-
In summary, hashing is a versatile and essential concept in computer science and
cryptography. It is used to enhance data security, ensure data integrity, and optimize data
retrieval processes. Understanding the right hashing techniques and choosing appropriate
hash functions is vital for the success of many applications in information technology and
data security.

You might also like