|
Hashing
e.g. keys are words with 8 letters
# of possible keys = 268 ~ 2 x 1011 ~ 200 Giga
names → table sparse
SMITH
CHEN
Hashing functions must be
| ↑ | ↑ | ↑ | ||||||||||||
e.g.
| 6 | 2 | 5 | 3 | 8 | 1 | 9 | 4 | ↑ | ↑ | ↑ |
fast but may not be even
b) folding
e.g. 625 | 381 | 94
| 625 |
| 381 |
| 94 |
| -------- |
| 1100 |
spread the keys better
c) Modular Arithmetic
R -- list contains 3-letter month name and number of days
3.1
h (KEY ) -- convert first and second letters to ASCII codes, add them, and mod 13
| h(JAN) | = ( ASCII ( J ) + ASCII ( A ) ) mod 13 |
| = ( 74 + 65 ) mod 13 | |
| = 139 mod 13 | |
| = 9 |
| h(AUG) | = ( 65 + 85 ) mod 13 |
| = 150 mod 13 | |
| = 7 |
| h(FEB) | = ( 70 + 69 ) mod 13 |
| = 139 mod 13 | |
| = 9 |
keys map to same integer are called synonyms
( JUN, JUL, OCT ), ( AUG, DEC ), ( JAN, FEB, SEP )
| index i | KEY | h(KEY) | # comparisons |
|---|---|---|---|
| 0 | MAY | 12 | 2 |
| 1 | NOV | 1 | 1 |
| 2 | APR | 2 | 1 |
| 3 | JUN | 3 | 1 |
| 4 | JUL | 3 | 2 |
| 5 | OCT | 3 | 3 |
| 6 | |||
| 7 | AUG | 7 | 1 |
| 8 | DEC | 7 | 2 |
| 9 | JAN | 9 | 1 |
| 10 | FEB | 9 | 2 |
| 11 | SEP | 9 | 3 |
| 12 | MAR | 12 | 1 |
Average # of comparisons
1.67
it depends on table density and quality of hashing function rather than size