This article is a Hash table example.
Having an array r[] , you know that: r=&r[0] , r+1=&r[1], r+2=&r[2] ...and so on. The pointers r, r+1, r+2... are called the indexes of the elements of the array and *r=r[0], *(r+1)=r[1], *(r+2)=r[2] and so on.
The problem:" Having an array, index the elements of the array in accordance to a specified correspondence".
An example: Given the array r={30,31,32,33,62} we will index the elements of the array in accordance to the remainder of the division of the elements by 30.
We have:
Element remainder index result
30 30%30=0 r+0 *(r+0)=30 correct!
31 31%30=1 r+1 *(r+1)=31 correct!
32 32%30=2 r+2 *(r+2)=32 correct!
33 33%30=3 r+3 *(r+3)=33 correct!
62 62%30=2 r+2 *(r+2)=62 ??? false!!!
The element of the array play the rolle of a key. We insert the key to a function, called
Hash function that produces the index of the element.
The process is called Hashing , the Hash keys are not needed to be exclusively the elements of the array but may be any data type refered to the elements of the array (a float, a string, a structure etc). The collection of the produced indexes is called a Hash table.
Now back: As you see we have two different keys 32 and 62 that produce throught our Hash function the same index , r+2. The phenomenon is called a collision.
To avoid collision we prefer to index not a simple array but an array of arrays or an array of Linked lists (An array of linked lists is preferable because we can add new nodes at a linked list "on the fly" inserting at these new nodes the elements of the array that produce collisions).
So indexing this array of arrays: int *(a[10])[2]={{30},{31},{32,62},{33}} and using the same as above Hash (function? No!) criterion :
unsigned int Hash_function (unsigned int key)
{
return (key%30)
}
we can find (with what Hash function?) the indexes : a[0] , a[1], a[2] , a[2]+1 , a[3] .
Indeed, *a[0]=30 , *a[1]=31 , *a[2]=32 , *(a[2]+1)=62 , *a[3]=33 avoiding collisions.
So have a look a t the result and study about the C code to understand.
Example
Given the array int a[4]={33,32,31,30}; index the elements of the array in accordance to the remainder of the division of the elements by 30.
Our Hash function is the:
int * Hash_function (int r)
{
int* p ;
p=a+3-r;
printf("the element divided by 30 and having remainder %u has index a+%u and is the %u\n\n",r,3-r,*p);
return p;
}
Our keys are the integers r, the remainders of the divisions of the elements by 30.
we have:
key=0 index a+3
key=1 index a+2
key=2 index a+1
key=3 index a+0
The C code Result at Hyperterminal
Further reading :
https://www.cs.auckland.ac.nz/software/AlgAnim/hash_tables.html
http://www.cs.uregina.ca/Links/class-info/210/Hash/ |