Database Systems · Hash Functions and Double Hashing
Where this fits
A hash table stores a record at an address computed from its key: gives the home bucket. Two independent problems must be solved:
- Which function ? — it must spread keys uniformly over and be cheap to compute.
- What if two keys land in the same bucket? — collision resolution: chaining, or open addressing (linear probing, quadratic probing, double hashing).
This chapter covers the hash-function choice and double hashing in full, plus the clustering that probing causes.
Notation (state it once, use it everywhere)
| Symbol | Meaning |
|---|---|
| number of buckets (table size); valid addresses | |
| the key (integer, or a string mapped to an integer) | |
| home address, | |
| primary hash function (gives the home slot) | |
| secondary/step hash function (gives the probe step) | |
| probe number, | |
| load factor ( = keys stored) |
Convention warning (0-based vs 1-based). Standard CS texts number buckets and probes . Unless a question says otherwise, use 0-based, probe starting at . If a question numbers slots , subtract 1, compute, add 1 back.
3.1 Division method (the GATE favourite)
Rule: choose prime, and not close to a power of 2 or 10. Reason: if , then depends only on the low bits of — keys differing only in high bits collide. If , only the last decimal digits matter. A prime with no small factors mixes all digits.
Worked example 3.1. (prime), keys .
- : , → 12
- : , → 2
- : , → 12
- : , → 12
Three keys collide at 12 — a bad key set, not a bad function; with prime the residues are still spread over the full range for random keys.
Worked example 3.2 (why prime matters). , keys (all ): every one gives . With : , , , — all distinct. Exam line: "division method needs prime ; power-of-two is a trap."
3.2 Mid-square method
Square the key, take the middle bits/digits as the address, where .
Worked example 3.3. Key , table of buckets (so take the middle 3 decimal digits).
. Compute: ; ; total . 9740641 has 7 digits, so the middle three are digits 3–5: 406 → . (Texts differ on how to centre when the digit count is odd; state your convention.)
Worked example 3.4 (binary). , so bits. (8 bits). Middle 4 bits = 1010 → .
3.3 Folding method
Split the key into equal parts and add the parts (shift folding), or reverse every alternate part before adding (boundary folding), then take .
Worked example 3.5. , parts of 3 digits: . Sum . With : → .
Worked example 3.6 (boundary folding). Same key; reverse alternate parts: . Sum ; → .
3.4 Hashing strings / non-numeric keys
A string must first be turned into an integer. Two standard ways:
(a) Sum of character codes — weak (anagrams collide: "abc" and "cab" give the same sum).
(b) Polynomial / Horner's rule — the standard good method: where is the code of the -th character, the length, a base (often 31, 33, or 128). Evaluate left-to-right with Horner: — this keeps numbers small.
Worked example 3.7. "CAT", ASCII , , , base , .
Horner: start .
→ . (Check the polynomial form: ; . ✓)
Worked example 3.8 (anagram trap). "ABC" codes : sum ; "CBA" codes : sum — same under method (a). Under Horner with : "ABC" ; "CBA" . Different. ✓ Use polynomial hashing for strings.