File organisation and access

Computer Science A2 · Data Representation. The idea in plain words, the rules to remember, the mistake to avoid and a worked example, written and checked by Mega Lecture.

Test this topic freeAll Computer Science A2 topics

The idea

File organisation is how the records are arranged in a file. In a serial file they are in the order they were added. In a sequential file they are in order of a key field. In a random file each record is placed at an address calculated from its record key.

File access is how a record is reached. Sequential access reads from the start, one record after another. Direct access goes straight to the record.

A hashing algorithm turns a key into an address. It is used to write a record to a random file and again, with the same key, to find it. Two keys can give the same address, which is a collision.

In simpler words

Files can store records in different orders. A serial file keeps records in the order they arrived. A sequential file keeps them sorted by a key, like an ID number. A random file puts each record at an address worked out from its key. Hashing is the sum that turns a key into an address. Sometimes two keys give the same address. That is a collision.

An everyday comparison

A serial file is like a pile of letters in the order they came. A sequential file is like a register sorted by roll number. A random file is like a car park where your number plate decides your bay, so you go straight there. The comparison stops working because two cars can be given the same bay by the hash, and the file needs a rule to deal with that collision.

Rules to remember

Common mistake

Students mix up serial and sequential. Serial means no order except the order of arrival; sequential means sorted by a key field. A search of a sequential file can stop as soon as a larger key is read, but a serial file must be read to the end.

In the exam

Name the organisation and the access method separately. Justify the choice with the use: sequential for batch jobs that process every record, such as payroll; random for finding one record at once, such as bookings. For hashing, write the algorithm, for example address = key MOD number of positions, then work the address for the given key. Explain a collision and give a fix: the next free position or an overflow area. Mention that a sequential search can stop early once a larger key is read.

Worked example

A random file has 13 record positions numbered 0 to 12. The hash function is Key MOD 13 and a collision is resolved by using the next free position. The file is empty. Records with keys 150 and 59 are stored in that order. Where is each one stored?

  1. 150 MOD 13: 13 × 11 = 143 and 150 − 143 = 7, so key 150 goes to position 7.
  2. 59 MOD 13: 13 × 4 = 52 and 59 − 52 = 7, so key 59 also hashes to position 7. This is a collision.
  3. Position 7 is full, so the next position, 8, is tried. It is free.

Answer: Key 150 is in position 7 and key 59 is in position 8.

Ask about this

Type what you are stuck on. The helper finds the explanation, worked examples and the notes to read, free and instant.

Keep going

← User-defined data typesFloating-point numbers, representation and manipulation →Take a Computer Science A2 test