The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Hashing

Hashing is a technique which can be understood from the real time application.

Assuming a class of 50 members, Each students has their Roll number in the range from 1 to 50. Now you the C Programmer collects all the students details using array from array[1] to array[50]. array[0] is neglected. Assuming a class of 50 members, Each students has their Roll number in the range from 1 to 50. Now you the C Programmer collects all the students details using array from array[1] to array[50]. array[0] is neglected. Now, assuming a next class of 50 members, Each students has their Roll number in the range from 101 to 150. Actual Programmer will initialize array[151] and waste the memory space from array[0] to array[100] and then collects all the students details from array[101] to array[150]. But you the C Programmer with more knowledge in data structure will divide every students roll no with 100 and then store the result from array[1] to array[50]. This technique is called as hashing.

Types - Hashing

  • Division Method
  • Multiplication Method
  • Mid-Square Method
  • Folding Method

Advantages - Hashing

  • More memory spaces are saved for future use.
  • Easy to retrive the data from the array.
  • Fastest in searching the elements of student roll no in an arrays and lists.

Disadvantages - Hashing

  • Very often collisions will occur.

C program - Hashing

Here is the C program to demonstrate Hashing.

hashing.c
#include <stdio.h>
int main()
{
int arry[100], arry1[100], n, result = 0, i, count = 0;
printf("Enter the size of an array : ");
scanf("%d",&n);
printf("\nEnter %d elements : \n",n);
for(i = 0; i < n; i++)
{
scanf("%d",&arry[i]);
}
for(i = 0; i < n; i++)
{
result = arry[i] % 10;
arry1[result] = arry[i];
printf("\nlocation = arry1[%d], value = %d ",result, arry1[result]);
result = 0;
}
return 0;
}
Enter the size of an array : 5
Enter 5 elements
101 102 103 104 105
location = arry1[1], value = 101 
location = arry1[2], value = 102
location = arry1[3], value = 103
location = arry1[4], value = 104
location = arry1[5], value = 105

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report