📑 Contents

Chapter 13.2: File Organization and Access

9618 Computer Science

📚 Learning Objectives
📖 Prior Knowledge Required
🌟 Did You Know?

Computers are used to access vast amounts of data and to present it as useful information. Millions of people expect to be able to retrieve the information they need in a useful form when they ask for it. This information is all stored as data in files, everything from bank statements to movie collections. In order to be able to find data efficiently, it needs to be organized.

1. File Types

In everyday computer usage, a wide variety of file types is encountered. Examples are graphic files, word-processing files, spreadsheet files and so on. Whatever the file type, content is stored using a specific binary code that allows the file to be used as intended. There are only two defined file types.

1.1 Text Files

📖 Definition: Text File

A text file contains data stored according to a character code of type. It is possible, by using a text editor, to create a text file to be used as input to a program.

📝 Organization of Text Files

1.2 Binary Files

📖 Definition: Binary File

A binary file is designed for storing data to be used by a computer program. Binary files are stored as a sequence of bytes representing any data including text, numbers, images, and sounds.

📝 Organization of Binary Files
Text File vs Binary File TEXT FILE • Sequence of characters • Human-readable • Uses ASCII encoding • .txt, .csv files BINARY FILE • Sequence of bytes • Not human-readable • Records with fields • .exe, .jpg files

2. File Organization Methods

The way records are arranged within a file is called the file's organization method. Choosing the right method affects how quickly data can be searched, added, updated, or deleted.

2.1 Serial File Organization

📖 Definition: Serial File Organization

A method of file organization in which records of data are physically stored in a file, one after another, in the order they were added to the file.

Example: Logging sensor data at a remote weather station. As each transaction is added to the file in the order of arrival, these records will be in chronological order. Storing customer meter readings for gas or electricity before they are used to send bills to all customers.
Serial File Organization (Chronological Order) Record 1 (First added) Record 2 Record 3 Record 4 (New) ... New records appended Start of file
🌟 When to Use Serial Files?

2.2 Sequential File Organization

📖 Definition: Sequential File Organization

A method of file organization in which records of data are physically stored in a file, one after another, in a given order. The order is usually based on the key field of the records as this is a unique identifier.

Example: A file used by a supplier to store customer records for gas or electricity in order to send regular bills to each customer. All records are stored in ascending customer number order, where the customer number is the key field that uniquely identifies each record.
Sequential File Organization (Ordered by Key Field) Before: Records sorted by Customer Number Cust #1 Record 1 Cust #2 Record 2 Cust #4 Record 4 Cust #7 Record 7 Cust #8 Record 8 After: Customer #5 Added in Correct Position Cust #1 Cust #2 Cust #4 Cust #5 NEW! Cust #7 Cust #8 ...
💡 Key Difference: Serial vs Sequential

2.3 Random File Organization

📖 Definition: Random File Organization

A method of file organization in which records of data are physically stored in a file in any available position. The location of any record in the file is found by using a hashing algorithm on the key field of a record.

Example Use Cases: Bank account access where individual customer records need instant lookup, real-time stock updates where specific records are accessed frequently, database systems requiring fast individual record access.
Random File Organization (Hashing Algorithm) Key Field Key: 3024 Hashing 3024 MOD 2000 = 1024 Address Addr: 1024 File Storage (Random Positions) Cust #8 Cust #2 Empty Cust #4 Cust #3 Cust #1 ...
Organization Type Order Best Use Case
Serial Chronological (arrival) Log files, temporary transaction files
Sequential Sorted by key field Payroll, billing, batch processing
Random Calculated by hashing Real-time systems, bank accounts

3. File Access Methods

File access is the method used to physically find a record in the file. There are two main methods of file access: sequential access and direct access.

3.1 Sequential Access

📖 Definition: Sequential Access

A method of file access in which records are searched one after another from the physical start of the file until the required record is found. This method is used for serial and sequential files.

📝 How Sequential Access Works

For Serial Files:

For Sequential Files:

Example: If searching for Customer 6 in a sequential file sorted by customer number, each record would be read until Customer 7 was reached. Then it would be assumed that Customer 6 was not stored in the file. No need to search further!
Sequential Access: Searching for Customer 6 Cust #1 ✓ Check Cust #2 ✓ Check Cust #4 ✓ Check Cust #5 ✓ Check Cust #7 7 > 6 STOP! No need to search Search stops here - Customer 6 not found Result: Customer 6 record not in file
⚠️ Important: Hit Rate

Sequential access is efficient when every record in the file needs to be processed, for example, a monthly billing or payroll system. These files have a high hit rate during processing as nearly every record is used when the program is run.

3.2 Direct Access

📖 Definition: Direct Access

A method of file access that can physically find a record in a file without other records being physically read. Both sequential and random files can use direct access. This allows specific records to be found more quickly than using sequential access.

📝 How Direct Access Works

For Sequential Files:

For Random Files:

Example: When a single customer record needs to be updated when the customer's phone number is changed. Here, the file being processed has a low hit rate as only one of the records in the file is used.
Direct Access Methods Sequential File with Index INDEX Key: 1 → Addr 100 Key: 2 → Addr 200 Key: 3 → Addr 300 ... FILE Addr 100 Addr 200 Addr 300 ... Random File with Hashing KEY e.g. 3024 HASH 3024%2000=1024 Addr FILE (Random Positions) Direct access to address 1024
Access Method Used With Best For Hit Rate
Sequential Serial & Sequential files Batch processing, payroll, billing High
Direct Sequential (with index) & Random files Real-time systems, individual record lookup Low

4. Hashing Algorithms

📖 Definition: Hashing Algorithm

A hashing algorithm is a mathematical formula used to perform a calculation on the key field of a record. The result of the calculation gives the address where the record should be found.

4.1 How Hashing Works

📝 Hashing Algorithm Example

If a file has space for 2000 records and the key field can take any values between 1 and 9999, the hashing algorithm could use:

Address = (Key Field MOD File Size) + Start Address

Example Calculation:

Hashing Algorithm: Key → Address Key Field 3024 MOD Operation 3024 ÷ 2000 Remainder 1024 Address 1024 The remainder gives the storage location in the file

4.2 Types of Hashing Algorithms

Method Description Example
Modulo Division Most common method: Key MOD TableSize 3024 MOD 2000 = 1024
Folding Breaks the key into parts and adds them together 30 + 24 = 54
Mid-Square Square the key and use middle digits as address 3024² = 9144576 → 445
Truncation Use only part of the key 3024 → Use last 3 digits: 024
🌟 Properties of Good Hashing Algorithm

4.3 Collision Handling

📖 Definition: Collision

A collision occurs when the same address is calculated for different key field values. This happens when different keys produce the same remainder when divided by the file size.

⚠️ Collision Example

Using the hashing algorithm: Address = Key MOD 2000

Collision: Two Keys, Same Address Key: 3024 Key: 5024 MOD 2000 = 1024 Address 1024 💥 COLLISION!

4.4 Collision Resolution Strategies

📝 Strategy 1: Open Hash (Linear Probing)
📝 Strategy 2: Closed Hash (Overflow Area)
📝 Strategy 3: Chaining
💡 Exam Tip: Key Verification

When reading a record using direct access:

  1. Calculate address using hashing algorithm
  2. Read record at that address
  3. Check key field matches the original key
  4. If not matching, apply collision resolution (search next location or overflow area)

4.5 Hashing for Non-Numeric Keys

Hashing algorithms can also be used to calculate addresses from names and other non-numeric data.

📝 Converting Text to Address
  1. Look up the ASCII code for each character in the name
  2. Add all the ASCII values together
  3. Divide the sum by the number of locations in the file
  4. Use the remainder as the address
Example: Converting name "ABC" to an address for a file with 1000 locations:
Hashing Non-Numeric Key: "ABC" Name ABC ASCII Values 'A' = 65 'B' = 66 'C' = 67 Sum = 65+66+67 = 198 198 MOD 1000 = 198 Address 198 Always verify the key field matches when reading back!
❌ Common Mistakes

5. Key Takeaways

📌 Summary Points

File Organization

File Access

Hashing

Scenario Best Organization Best Access
Monthly payroll processing Sequential Sequential
Log file for sensor data Serial Sequential
Bank account lookup Random Direct
Customer billing system Sequential Sequential/Direct
Real-time stock updates Random Direct

6. Exam-Style Questions

1. Explain the difference between serial and sequential file organization. Give an example of when each would be appropriate. [6 marks]

Answer:

  • Serial file: Records stored in order of arrival/chronological order, no sorting required
  • Sequential file: Records stored in sorted order based on a key field
  • Serial files have no defined order, while sequential files have a logical sequence based on key field
  • Serial example: Log files, temporary transaction files, sensor data recording
  • Sequential example: Payroll systems, customer billing, exam results processing

Additional points for deeper understanding:

  • Serial files are simpler to implement but slower to search
  • Sequential files allow early termination of search when target is passed
  • Sequential files require re-sorting when new records are added
2. A file stores 1000 records. The key field values range from 1 to 9999. A hashing algorithm uses MOD 1000 to calculate the address. Calculate the address for a record with key field value 7563. [2 marks]

Answer:

  • Address = 7563 MOD 1000
  • 7563 ÷ 1000 = 7 remainder 563
  • Address = 563
3. Explain what is meant by a collision in hashing, and describe two methods for handling collisions. [6 marks]

Answer:

Collision: When two different key field values produce the same address when a hashing algorithm is applied.

Methods for handling collisions:

  • Open hash (Linear probing): Store the record in the next available free space. When reading, check the calculated address then subsequent addresses until match found.
  • Closed hash (Overflow area): Set up a separate overflow area. When collision occurs, store record in next free space in overflow area. Search overflow area if key doesn't match.
  • Chaining: Store all records with same calculated address in a linked list. Each address has a pointer to a chain of records.
4. Describe the difference between sequential access and direct access. When would each be appropriate? [5 marks]

Answer:

  • Sequential access: Records searched one after another from start of file until required record found
  • Direct access: Record found without reading other records; can jump directly to specific location
  • Sequential appropriate: High hit rate, batch processing, payroll/billing systems, reading entire file
  • Direct appropriate: Low hit rate, individual record lookup, real-time systems, bank account access
  • Sequential used with serial and sequential files; Direct used with sequential (with index) and random files
5. A binary file is to be used to store data for a program. (a) State the terms used to describe the components of such a file. (b) Explain the difference between a binary file and a text file. [5 marks]

Answer (a):

  • Record: A collection of related data fields representing one item
  • Field: A single piece of data within a record (e.g., name, ID number)

Answer (b):

  • Text file: Contains character data formatted into lines; has end-of-line and end-of-file characters; human-readable; uses ASCII encoding
  • Binary file: Data stored in internal representation; contains records with defined format; no field separator characters needed; not human-readable
  • Text files can be opened with text editors; binary files require specific programs

6. Exam-Style Questions (continued)

6. Explain how a hashing algorithm could be used to calculate an address from a person's name "JONES". The file has space for 500 records. [4 marks]

Answer:

  • Find ASCII value for each character: J=74, O=79, N=78, E=69, S=83
  • Add values together: 74 + 79 + 78 + 69 + 83 = 383
  • Divide by file size and find remainder: 383 MOD 500 = 383
  • Address = 383

Additional points:

  • Must verify the key field matches when reading back from this address
  • If collision occurs, apply collision resolution strategy
7. A company needs to choose a file organization method for their customer database. Customers frequently need to look up their account details individually. Which file organization and access method would be most appropriate? Justify your answer. [4 marks]

Answer:

  • Use random file organization with direct access
  • Reason: Individual customer records need to be accessed frequently (low hit rate)
  • Direct access allows quick lookup of specific records without searching through entire file
  • Hashing algorithm can calculate exact location from customer ID/account number
  • More efficient than sequential access for individual lookups
8. Describe three properties of a good hashing algorithm. [3 marks]

Answer:

  • Quick to calculate: The algorithm should be computationally efficient
  • Even distribution: Records should be spread evenly across the available address space
  • Minimize collisions: Should not generate addresses that cluster, causing frequent collisions
  • Cover complete range: Should use the full address space available in the file
9. A file is stored at address 500. Each record takes up 5 locations and there is space for 1000 records. The key field values range from 1 to 9999. Using the hashing algorithm: Address = Start Address + (Key MOD FileSize) × RecordSize, calculate the address for a record with key field value 9354. [3 marks]

Answer:

  • Remainder: 9354 MOD 1000 = 354
  • Offset: 354 × 5 = 1770
  • Address: 500 + 1770 = 2270

If collision occurs with open hash, next location would be:

  • Next address = 2270 + 5 = 2275
10. Compare the use of serial, sequential, and random file organization for the following scenarios. Justify each choice: (a) Recording daily rainfall readings at a remote weather station, (b) Providing annual tax statements for employees, (c) Real-time bank ATM transactions. [6 marks]

Answer:

(a) Remote weather station:

  • Serial - Data recorded in chronological order as it arrives
  • No need for sorting; data collected in real-time
  • Simple to append new readings; processed later in batch

(b) Annual tax statements:

  • Sequential - Employee records sorted by ID/key field
  • High hit rate - processing all employees at end of year
  • Efficient batch processing for generating all statements

(c) Bank ATM transactions:

  • Random - Individual account access required
  • Low hit rate - only one account accessed per transaction
  • Direct access using account number for fast lookup

7. Glossary

Serial File Organization → A method of file organization in which records are stored one after another in the order they were added to the file (chronological order).

Sequential File Organization → A method of file organization in which records are stored one after another in a given order, usually based on the key field.

Random File Organization → A method of file organization in which records are stored in any available position, with locations calculated using a hashing algorithm on the key field.

File Access → The method used to physically find a record in a file.

Sequential Access → A method of file access in which records are searched one after another from the start of the file until the required record is found.

Direct Access → A method of file access that can find a record without reading other records, using an index or hashing algorithm.

Hashing Algorithm → A mathematical formula used to calculate the storage address of a record from its key field.

Collision → When two different key field values produce the same address when a hashing algorithm is applied.

Open Hash (Linear Probing) → A collision resolution method where records are stored in the next available free space.

Closed Hash (Overflow Area) → A collision resolution method where a separate overflow area is used to store records that collide.

Key Field → A unique identifier field in a record used for searching and organizing data.

Hit Rate → The proportion of records accessed during a processing run. High hit rate = most records accessed; Low hit rate = few records accessed.

Text File → A file containing data stored as characters according to a character code (e.g., ASCII), human-readable.

Binary File → A file containing data in internal representation format, not human-readable, organized as records with fields.

8. Exam Success Tips

💡 Remember: Serial vs Sequential
💡 Remember: Access Methods
🧠 Memory Trick: File Organization
💡 Hashing Formula

Most common hashing method:

Address = Key MOD FileSize

Example: 3024 MOD 2000 = 1024 (address)

❌ Common Mistakes to Avoid

8. Exam Success Tips (continued)

🧠 Collision Resolution Methods
💡 Answer Structure Tips
⚠️ Must Mention When Asked About...
🌟 Quick Reference: Decision Tree
📌 Final Exam Checklist