UNIHIVE

Study Hub UNIPORT CSC 283

CSC 283 · Information Systems and File Structures

Direct (random) files and hashing

Relative files, hashing a key to an address, collisions and how to handle them, and load factor.

Our notes

2 min read

The 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.

Keykey mod 11
275
187
297 (collision with 18)
286
396 (collision with 28)
132
165 (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:

  1. 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.
  2. Chaining: each address holds a linked list of all records that hash there.
  3. Buckets: each address is a block holding several records; overflow only when the bucket is full.
  4. 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.

All CSC 283 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.