Sub topic 5
Sorted Files: Binary Search and Costly Updates
Assume you know records, disk blocks, blocking factor, binary search, and logarithms. This chapter treats a file whose records are stored in ascending order of a search field, such as student_id.
Working model
A sorted file keeps records ordered by a chosen field. If the file contains \(b\) data blocks, a search can inspect blocks by binary search rather than scanning from the beginning.
For fixed-length, unspanned records:
\[ \text{blocking factor} = \left\lfloor \frac{B}{R} \right\rfloor \]
where \(B\) is the block size and \(R\) is the record size. The number of data blocks is:
\[ b = \left\lceil \frac{r}{\text{blocking factor}} \right\rceil \]
where \(r\) is the number of records.
A block-level binary search needs approximately:
\[ \lceil \log_2 b \rceil \]
block accesses. The exact count can vary by one depending on whether the initial block and unsuccessful-search checks are counted.
| Operation | Typical sorted-file cost |
|---|---|
| Equality search on ordering field | \(O(\log b)\) block accesses |
| Range search | \(O(\log b + s)\), where \(s\) is the number of result blocks |
| Insert while preserving order | Search cost plus shifting or rewriting blocks |
| Delete while preserving order | Search cost plus possible compaction |
Why binary search works
The search field must be ordered across the blocks. Suppose the first keys in the blocks are:
Block 0: 10, 14, 18
Block 1: 24, 29, 31
Block 2: 40, 44, 49
Block 3: 57, 61, 66To find key 44, compare it with a suitable middle block, eliminate the impossible half, and continue. Once the correct block is found, scan the records inside that block.
A range query benefits even more. First locate the lower bound with binary search. Then read consecutive blocks until the upper bound is passed. Sequential block reads are usually efficient.
Worked calculation
Suppose \(B=1024\) bytes, \(R=100\) bytes, and \(r=30{,}000\) records.
\[ \text{blocking factor}=\left\lfloor\frac{1024}{100}\right\rfloor=10 \]
\[ b=\left\lceil\frac{30{,}000}{10}\right\rceil=3{,}000 \]
The binary-search estimate is:
\[ \lceil\log_2 3000\rceil=12 \]
Thus, finding a record by the ordering field needs about 12 block reads, followed by a small scan within the final block.
import math
def sorted_file_cost(block_size, record_size, record_count):
blocking_factor = block_size // record_size
data_blocks = math.ceil(record_count / blocking_factor)
binary_search_reads = math.ceil(math.log2(data_blocks))
return blocking_factor, data_blocks, binary_search_reads
bfr, blocks, reads = sorted_file_cost(1024, 100, 30_000)
print("blocking factor:", bfr)
print("data blocks:", blocks)
print("binary-search estimate:", reads)Why updates are costly
Insertion is efficient only if the new record belongs at the end or if free space exists in the correct block. Otherwise, records may need to move to later blocks. This can cascade through much of the file.
Deletion can leave gaps. A system may tolerate those gaps, mark records as deleted, or reorganize the file later. Immediate compaction preserves dense ordering but increases write cost.
A sorted file is therefore a good choice when searches and ordered scans are frequent and updates are relatively rare. A heap file usually inserts more cheaply, while a hash file is often better for equality searches when ordering and range queries are unnecessary.
Exercise
A sorted file has 1,024 data blocks. What is the binary-search estimate for locating a block by its ordering field, and why can insertion still be expensive?
Show answer
The estimate is log2(1,024) = 10 block accesses. Insertion can be expensive because records or whole blocks after the insertion point may need to shift to preserve sorted order.