Information Technology Topic

File Organisation Methods

  1. Read notes
  2. Flashcards
  3. Quiz
  4. Confident

Study focus - 3 things

  1. File organisation is how the records are arranged in storage. Sequential: one after another in key order. Random: each record's slot is worked out from its key. Index-sequential: records in key order plus an index that points to them
  2. To find record k in a sequential file you read k records; in a random file you read 1; in the simple index-sequential model you take 2 steps (one index look-up, one read). The average for a sequential file of n records is (n + 1) ÷ 2
  3. Every organisation is a trade-off: sequential is simple but slow to find one record, random is fast for one record but not in key order, and index-sequential does both jobs at the cost of an index to store and keep up to date

Visual notes

The same six records in three layouts
A sketch with three panels showing the same six student records, with keys 118, 219, 320, 421, 522, 623. Panel A, sequential: the six records sit side by side in key order. To reach 421 the program reads 118, 219, 320, 421, which is 4 reads. Panel B, random: eight numbered slots hold the records at positions worked out from the key, and slots 2 and 5 are empty. Key 421 divided by 8 leaves remainder 5, so it is in slot 6, reached in 1 read. Panel C, index-sequential: an index row of the six keys has an arrow from each key to its record in a row of records in key order. Finding 421 takes 1 index look-up and 1 read, which is 2 steps.

The same six records, three ways. Record 421 takes 4 reads in the sequential layout, 1 read in the random layout and 2 steps in the index-sequential model.

Section 1Files, records, fields and keys

A file is a collection of records about one kind of thing, for example one record per student. A record holds all the facts about one student. Each single fact is a field, such as the surname or the date of birth. One field is chosen as the key field. Its value is different for every record, so it identifies the record: a student number, an employee number, a customer number or a product code. File organisation means how the records are arranged in storage. The syllabus names three: sequential, random and index-sequential. How a program then reaches a record (its access method) is a separate question, covered in the next topic.

Section 2Sequential organisation

In a sequential file the records are stored one after another, in order of the key field. That is also called sequential field ordering: each record's key is bigger than the key of the record before it. To find student 421 you start at the first record and go through them in order until you reach it. There is no shortcut. Adding a record in the middle is awkward, because the order must be kept. The usual method is to make a new, updated copy of the file with the new record in its place, or to merge a small sorted batch of new records into the old file. A sequential file can be sorted and merged as a whole. The big plus: it is simple, it needs no index and no spare slots, and it is excellent when every record is going to be processed in order.

Section 3Random organisation

In a random file the position of each record is worked out from its key. The program does a calculation on the key and goes to the slot the calculation gives. The records are not in key order, and some slots are left empty. The sketch below uses one simple rule: divide the key by 8, take the remainder, and add 1. Key 421 ÷ 8 is 52 remainder 5, so the record goes in slot 6. Key 219 gives slot 4. The same rule finds the record again later. (The six keys in the sketch were chosen so that no two share a slot.) Because the program goes straight to the slot, any record is reached in one read. The price: reading the records in key order is not natural, and empty slots use up space. Easy to mix up: 'random' does not mean records are picked by chance. It means any record can be reached directly.

Section 4Index-sequential organisation

An index-sequential file has two parts: the records, kept in key order like a sequential file, and an index. The index lists each key with the place where its record is stored, a bit like the index at the back of a book. To find a record by its key, the program looks the key up in the index and goes straight to the place it names. To print a list in order, it simply reads the records one after another in key order. So one file does both jobs. The cost: the index takes extra storage, and every time a record is added or deleted the index has to be updated to match, which makes writing slower. Index-sequential is the syllabus name. Some systems simply call this an indexed file.

Section 5Counting reads: a simple model

To compare the organisations, count the steps. Take a file of n records with keys in order, 1 to n. Sequential: finding record k means reading records 1 to k, so k reads. The best case is 1 read and the worst case is n reads. If every record is equally likely to be wanted, the average is (n + 1) ÷ 2. For 500 records that is 250.5 reads. Random: work out the slot, then read it: 1 read. Index-sequential: one index look-up, then one record read: 2 steps, however big the file. Real indexes are searched in cleverer ways, so treat 2 steps as a simple model for comparing, not an exact count.

Section 6Comparing the three side by side

Sequential: records in key order. Finding one record means passing the ones before it. No index, no empty slots. Easy to read everything in order, awkward to insert. Random: any record in one read. Not in key order, and empty slots are left over. Index-sequential: a record can be fetched directly by key and the records can also be read in key order. The cost is an index to store and keep up to date. Here's the trick: ask two questions about any file. Will I need to jump to one record quickly? Will I need to go through all the records in order? Sequential answers the second only, random the first only, and index-sequential answers both. Picking one for a real application is the job of the last topic in this section.

Examples

A library in Mandeville keeps 60 member records in a sequential file, in order of member number 1 to 60. (a) State how many records must be read to find member 1, member 25 and member 60. (b) State the best case and the worst case for finding any one member. (c) Calculate the average number of reads if every member is equally likely to be wanted. (d) State how many reads are needed to fetch any member if the file is organised as a random file.

(a) To reach record k you read records 1 to k, so k reads. Member 1: 1 read. Member 25: 25 reads. Member 60: 60 reads. (b) Best case: the wanted member is the first record, so 1 read. Worst case: the wanted member is the last record, so 60 reads. (c) Add the reads for every member: 1 + 2 + 3 + ... + 60 = 1830. Divide by the 60 members: 1830 ÷ 60 = 30.5 reads. This matches (60 + 1) ÷ 2 = 30.5. (d) In a random file the slot is worked out from the member number and read directly: 1 read, whichever member it is.

A school in Portmore keeps 500 student records keyed by student number 1 to 500. Compare a sequential file with an index-sequential file, using the simple model of one index look-up plus one read. (a) Find the reads needed to reach student 375 in the sequential file, and the steps in the index-sequential file. State how many fewer steps the index-sequential file needs. (b) A clerk looks up students 12, 250 and 480, one at a time, starting from the beginning of the sequential file each time. Find the total reads for the sequential file and the total steps for the index-sequential file. (c) At the end of term the school prints a list of all 500 students in student-number order. State which organisation is good enough for this and why.

(a) Sequential: 375 reads, because records 1 to 375 are read. Index-sequential: 1 index look-up + 1 read = 2 steps. The difference is 375 - 2 = 373 fewer steps. (b) Sequential: 12 + 250 + 480 = 742 reads. Index-sequential: 3 look-ups at 2 steps each = 6 steps. A random file would take 3 reads. (c) A plain sequential file is good enough. The list needs every record in key order, so the program reads the 500 records once from the first to the last, and none of that reading is wasted. An index would add storage and upkeep that this job does not need.

Now let's water it

Session runs notes to flashcards to quiz back-to-back.

Where this comes from