CSC 283 · Information Systems and File Structures
Our notes
2 min readThe idea
A direct (random) file lets the program go straight to a record's place without reading others. The address is worked out from the key.
Relative (self-addressing) files
If keys are numbers from 1 to N with few gaps, record k can be stored at relative position k. Example: hostel rooms 1 to 500. Perfect when keys are dense; wasteful when they are sparse (matric numbers like U2023/5570123).
Hashing
A hash function turns a key into an address (bucket number) in the range 0 to B − 1.
- Division (remainder) method: address = key mod B, with B a prime not close to a power of 2.
- Folding: split the key into parts and add them.
- Mid-square: square the key and take middle digits.
- Text keys are first turned into numbers (from character codes).
A good hash spreads keys evenly and is quick to compute.
Worked example: division method
B = 11 buckets (one record each). Keys 27, 18, 29, 28, 39, 13, 16.
| Key | key mod 11 |
|---|---|
| 27 | 5 |
| 18 | 7 |
| 29 | 7 (collision with 18) |
| 28 | 6 |
| 39 | 6 (collision with 28) |
| 13 | 2 |
| 16 | 5 (collision with 27) |
With linear probing (try the next slot): 27→5, 18→7, 29→8, 28→6, 39→6 full, 7 full, 8 full, so 9; 13→2, 16→5 full, 6 full, 7 full, 8 full, 9 full, so 10.
Final table: 2:13, 5:27, 6:28, 7:18, 8:29, 9:39, 10:16.
Collisions and synonyms
Two keys with the same home address are synonyms; the second causes a collision. Ways to deal with them:
- Open addressing: look for another slot in the table: linear probing (next slot), quadratic probing (1, 4, 9 slots on), double hashing (a second hash gives the step). Linear probing causes clustering.
- Chaining: each address holds a linked list of all records that hash there.
- Buckets: each address is a block holding several records; overflow only when the bucket is full.
- Overflow area: extra records go to a separate area with a pointer from the home bucket.
Load factor
Load factor α = records stored ÷ slots available. In the example, 7 ÷ 11 = 0.64. As α approaches 1, collisions and search times rise sharply; designers keep files around 0.7-0.8 full and rehash into a larger table when needed.
Strengths and limits
- Very fast single-record access (often one disk read).
- Records are not in key order, so a sorted report needs a separate sort.
- Deleting a record in open addressing must leave a marker, or later searches stop too early.
Exams usually ask
- Explain hashing and work out addresses with the division method.
- Resolve collisions by linear probing or chaining for given keys.
- Define load factor and discuss its effect.
Common mistake
Choosing B = 10 or 100 for the division method. Then the address is just the last digits of the key, and keys that share endings collide constantly; use a prime.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
No reading links here yet
Our team adds free books, videos and practice for each topic, and opens every link before it goes up.