Searching an array takes O(n) or O(log n) time. Hashing aims to find an item in nearly constant time by computing where it should be stored.

Hashing and Hash Tables

Hashing is a technique that maps a key, such as a roll number or a name, to a position in a table by means of a hash function. The table is called a hash table, and each position is a slot or bucket. To store an item, we compute h(key) and place the item in that slot. To find it, we compute h(key) again and look there. If the hash function spreads the keys well, insertion, search and deletion take O(1) time on average. Hash tables are used in compilers for symbol tables, in databases for indexes, in caches, and in dictionaries and sets of programming languages such as Python.

Definition: Hash function. A function that converts a key into an index of a hash table, h(key) = index, so that the item can be stored and found at that position.

Qualities of a Good Hash Function

A good hash function is easy and fast to compute, spreads keys uniformly over the table, and minimises collisions. It should use all parts of the key, so that similar keys give different indexes.

Types of Hash Functions

Division method: h(k) = k mod m, where m is the table size, best chosen as a prime not close to a power of 2. For m = 11, h(45) = 1. Mid-square method: square the key and take the middle digits. For k = 56, k² = 3136, and the middle digits 13 give the index. Folding method: split the key into parts and add them. For 123456 with parts 12, 34, 56, the sum is 102. Multiplication method: h(k) = floor(m × (kA mod 1)), with a constant A between 0 and 1. For string keys, the character codes are combined, for example by a polynomial sum.

Table 2.5: Hash function methods

Method Rule Example
Division k mod m 45 mod 11 = 1
Mid-square Middle digits of k² 56² = 3136 → 13
Folding Sum of parts of k 12 + 34 + 56 = 102
Multiplication floor(m(kA mod 1)) A ≈ 0.618

Example 2.5: Hashing by division

Problem: Insert the keys 25, 36, 47, 58, 69 into a table of size 11 using h(k) = k mod 11.

Solution:

  1. 25 mod 11 = 3, 36 mod 11 = 3, 47 mod 11 = 3, 58 mod 11 = 3, 69 mod 11 = 3.
  2. All five keys map to slot 3.

Result: Every key collides at slot 3. Keys that differ by a multiple of 11 always collide, so collision handling is needed.

#include <stdio.h>
int main(void) {
    int keys[] = {25, 36, 47, 58, 69, 12, 44, 89};
    for (int i = 0; i < 8; i++)
        printf("h(%d) = %d\n", keys[i], keys[i] % 11);
    return 0;
}

Output

h(25) = 3
h(36) = 3
h(47) = 3
h(58) = 3
h(69) = 3
h(12) = 1
h(44) = 0
h(89) = 1

Common mistakes

  • Choosing a table size that is a power of 2 with the division method, which uses only the low bits.
  • Expecting hashing to give sorted order. It does not.
  • Ignoring collisions, which always occur in practice.

Key takeaways

  • Hashing maps a key to a table index by a hash function.
  • Average search, insert and delete are O(1).
  • Methods: division, mid-square, folding and multiplication.
  • A good hash function is fast and spreads keys uniformly.

Practice questions

  1. What is a hash table?
  2. Give the division method.
  3. Name two uses of hashing.

Answers to the practice questions

  1. A table in which items are stored at positions computed from their keys by a hash function.
  2. h(k) = k mod m, with m the table size, preferably prime.
  3. Symbol tables in compilers and database indexes (also caches, dictionaries).

Sources

  • Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit II