Skip to content

Labs · 9618 A2 13.2

Hashing a File

Store records in a random-access file with key MOD n. Handle collisions with the next free slot or an overflow area, then find the records again.

7 levelsFree bench

In the exam

9618/31 May/June 2023 · Question 3 · 6 marks

A hashing algorithm uses key MOD 3 to find where records are stored in a random-access file. (a) Give the hash values of 1050 and 1025. (b) Describe what happens, when a record is stored or found, if the hash value is the same as one calculated before.

Points that gain marks

  • 1050 MOD 3 = 0 and 1025 MOD 3 = 2.
  • This is a collision: the storage place is already used by a record with a different key.
  • To store: search the file linearly from that place to the next free space (closed hashing), or search the overflow area linearly for the next free space (open hashing).
  • To find: search the same way, linearly from that place or through the overflow area, until the matching record key is found.
  • If it is not found, the record is not in the file.

In this lab: Levels 2 to 4 store records both ways; levels 5 and 6 find them, and stop at an empty place when a record is missing.

9618/31 October/November 2023 · Question 8(a) · 5 marks

Complete the pseudocode that reads one customer's record from a random file. The function Hash() takes the account number and returns the record's place in the file.

Points that gain marks

  • OPENFILE AccountFile FOR RANDOM
  • INPUT Customer.AccountNumber
  • Location ← Hash(Customer.AccountNumber)
  • SEEK AccountFile, Location: go straight to that place in the file.
  • GETRECORD AccountFile, Customer, then CLOSEFILE AccountFile.

In this lab: Hash() is the key MOD n of this lab. SEEK goes straight to the home slot, so a record is found without reading the whole file.

More real experiments

Log in or create account

IGCSE, A-Level & AP