Passer au contenu

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

Exam questions on what this lab teaches, with the points that gain marks.

9618/31 May/June 2023 · Question 36 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

  1. 1050 MOD 3 = 0 and 1025 MOD 3 = 2.
  2. This is a collision: the storage place is already used by a record with a different key.
  3. 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).
  4. To find: search the same way, linearly from that place or through the overflow area, until the matching record key is found.
  5. 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

  1. OPENFILE AccountFile FOR RANDOM
  2. INPUT Customer.AccountNumber
  3. Location ← Hash(Customer.AccountNumber)
  4. SEEK AccountFile, Location: go straight to that place in the file.
  5. 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.

The question as printed in the exam paper

More real experiments

All labs →

Se connecter ou créer un compte

IGCSE, A-Level & AP