Professional Documents
Culture Documents
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
i. Linear Probing
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.
It works similar to linear probing but the spacing between the slots is increased (greater than
one) by using the following relation.
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 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.
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.
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.