CSwithZain
WhatsApp Join a class

A Level 9618 · Paper 3 · 13.2

File organisation and access

How records are stored in a file, and how a program finds them again. Read the notes, try the past-paper questions, then check the mark scheme.

Past papers from November 2021 to June 202612 questions · 65 marks

    In this subtopic

    • Serial, sequential and random files, and choosing the right one
    • Sequential access and direct access
    • Hashing algorithms that store and find records, and what happens on a collision

    13.2 · 1 of 8Book page Contents

    Files, records and fields

    A file is a collection of records. Each record is a collection of fields, and every field holds a value. One field, the key field, holds a value that is unique to each record, so it can be used to identify and find the record.

    There are three ways to organise the records in a file: as a serial file, a sequential file or a random file.

    13.2 · 2 of 8Book page Contents

    Serial files

    A serial file is a collection of records with no defined order. Records enter the file in chronological order: each new record is simply added to the end of the file. All the records have the same format so they can be input and output correctly.

    A text file is an example of a serial file: characters are added one after another, in the order they are typed.

    Serial files are often used to record transactions as they happen, ready for batch processing later. Records can be added quickly because they do not need to be sorted.

    Advantages of serial organisation

    • It is simple.
    • It is cheap.
    • New records are added quickly, at the end of the file.

    Disadvantages of serial organisation

    • It is slow to find a record, because every record before it has to be read first.
    • On magnetic tape, space is wasted by the gaps between records.
    • It cannot give the fast access to single records that modern systems often need.

    13.2 · 3 of 8Book pages Contents

    Sequential files

    A sequential file stores its records in order of a key field. For the records to be sorted by key field, the key field must be unique and in order, but the values do not need to be consecutive.

    A record can be found by reading the key fields one by one until you reach the one you want. Because the records are sorted, you can stop as soon as you pass the place where the key would be, and faster searches such as a binary search can be used.

    Advantages of sequential organisation

    • The sorting makes it easy to find records.
    • A binary search can be used, which halves the number of records left to check at every step.

    Disadvantages of sequential organisation

    • Other records still have to be read while searching for a particular record.
    • It cannot give the fast access to single records that modern systems often need.
    • Adding a record means inserting it in the right place, which is slower than adding it at the end.

    Serial compared with sequential

    • In both, records are stored one after another and are accessed one after another.
    • Serial files are stored in chronological order; sequential files are stored in order of the key field.
    • In a serial file, new records are added in the next free space at the end of the file.
    • In a sequential file, new records are inserted in the correct position.

    13.2 · 4 of 8Book page Contents

    Random files and hashing

    A random file stores records in any available position. The location of a record is found by using a hashing algorithm on its key field.

    Hashing is the process of turning the key value of a record into the address where the record is stored. A hash function works out the address by doing a simple calculation on the key, or on parts of the key.

    Storing a record in a random file

    • The key field is hashed to give the address (the home location).
    • Check whether a record is already stored at that address.
    • If nothing is stored there, store the new record.
    • If another record is already stored there, search the next locations or the overflow area...
    • ...until a free space is found or the whole area has been searched.
    • If there is no space, output an error message.

    13.2 · 5 of 8Book page Contents

    Collisions

    A collision happens when the hashing algorithm gives the same address for two different keys. There are two ways to deal with it:

    1. Store the record in the next free location after its home location.
    2. Set up a separate overflow area and store the record in the next free space there.

    Note Some books call these two methods open hashing and closed hashing, but other books use the names the other way round. In the exam, describe the method instead of only giving its name.

    When a record is read using direct access, its address is calculated with the hashing algorithm and the record at that address is read. Before using it, its key field must be checked against the key you are looking for. If they do not match, the following records are read until a match is found, or the overflow area is searched.

    Finding a record in a random file

    • The key is hashed to give the address (the home location).
    • The key stored at that address is compared with the key being searched for.
    • If nothing is stored there, output "record not found".
    • If the keys are equal, the record has been found.
    • If the keys are not equal, search the next locations or the overflow area...
    • ...until the record is found or the whole area has been searched.
    • If the record is not found, output an error message.

    13.2 · 6 of 8Book page Contents

    Hashing algorithms

    The same hashing algorithm is used to write a record and to find it again, so a record can be read directly without searching the whole file. A good hashing algorithm is quick to calculate and spreads the records evenly, so there are few collisions.

    Method 1, the remainder (MOD): divide the key by the number of storage locations and use the remainder as the address. With 7 locations, numbered 0 to 6:

    Address ← Key MOD 7

    Worked example

    Store the records with keys 45, 17, 23 and 38.

    1. Step 1
      45 MOD 7 = 3, so record 45 is stored at address 3.
    2. Step 2
      17 MOD 7 = 3. Address 3 is already used, so this is a collision. Record 17 goes in the next free location, address 4.
    3. Step 3
      23 MOD 7 = 2, so record 23 is stored at address 2.
    4. Step 4
      38 MOD 7 = 3. Addresses 3 and 4 are used, so record 38 goes in address 5.
    5. Step 5
      To read record 17 later: 17 MOD 7 = 3. Address 3 holds key 45, which does not match, so read the next location. Address 4 holds key 17, so the record is found. If an empty location is reached first, the record is not in the file.
    Address0123456
    Key23451738

    Another way to deal with a collision is to put the record in a separate overflow area, which is searched in order.

    Method 2, folding: split the key into equal parts, add the parts together, then take the remainder when the total is divided by the number of locations. For key 452831 and 100 locations: 45 + 28 + 31 = 104, and 104 MOD 100 = 4, so the address is 4.

    The remainder method is the quickest, but keys that follow a pattern can clash. With 100 locations, Key MOD 100 only uses the last two digits, so 452831 and 993831 both go to address 31. Folding uses every part of the key (45 + 28 + 31 = 104 and 99 + 38 + 31 = 168), so these keys get different addresses (4 and 68).

    13.2 · 7 of 8Book page Contents

    Sequential and direct access

    There are two ways to reach a particular record in a file:

    • Sequential access: each record is read, one by one, until the one you want is found. Serial and sequential files are read this way. In a sequential file you can stop once the key you have reached is greater than the one you want.
    • Direct access: the program goes straight to the record. In a random file, the hashing algorithm gives the address of the record.

    Direct access can also be used with a sequential file, by creating a separate index file. Each entry in the index has two fields: the key field value, and the position of that record in the main file.

    Deleting and editing records. In a file that is accessed sequentially, deleting or editing a record means creating a new file: records are copied from the old file to the new one, with the change made on the way. In a direct-access file, a record can be edited or deleted in place, without making a new file.

    13.2 · 8 of 8Book page Contents

    Choosing a file organisation

    Which organisation to use

    • Serial files suit batch processing and backing up data, for example on magnetic tape.
    • Direct access (random files) suits a large file where single records must be found quickly, for example the file of passwords checked when users log in to a system with many users.
    • Sequential files suit jobs where many records are needed from one search of the file, for example a family history file where one search finds all the records with a particular family name.

    13.2

    Concepts in 13.2

    Every idea explained in 13.2, and where else in the notes it is used.

    ConceptIn one lineAlso used in
    collisionWhen the hashing algorithm gives the same address for two different keys.13.2 Hashing algorithms
    direct accessGoing straight to a record without reading the ones before it.13.2 Collisions, 13.2 Choosing a file organisation
    hashingTurning a record’s key into the address where the record is stored.13.2 Collisions
    hashing algorithmThe calculation that turns a key into a storage address, such as Key MOD 7.13.2 Random files and hashing, 13.2 Collisions, 13.2 Sequential and direct access
    index fileA file of key values and record positions that gives direct access to a sequential file.only here
    key fieldA field whose value is unique to each record, used to identify it.13.2 Sequential files, 13.2 Random files and hashing, 13.2 Collisions, 13.2 Sequential and direct access
    overflow areaA separate area of the file where records go when their home location is full.13.2 Random files and hashing, 13.2 Hashing algorithms
    random fileA file where each record is stored at an address worked out by hashing its key.13.2 Files, records and fields, 13.2 Collisions, 13.2 Sequential and direct access, 13.2 Choosing a file organisation
    remainder (MOD) methodA hashing algorithm: the address is the key MOD the number of locations.only here
    sequential accessReading records one by one until the one you want is found.only here
    sequential fileA file whose records are stored in order of the key field.13.2 Files, records and fields, 13.2 Sequential and direct access, 13.2 Choosing a file organisation
    serial fileA file whose records are stored in the order they arrive, with no sorting.13.2 Files, records and fields, 13.2 Sequential files, 13.2 Choosing a file organisation

    When you want more than reading

    The notes are the front door. The class is where it clicks.

    Join a class →Ask on WhatsApp