Block Search
Block Search combines indexing + sequential scan for large ordered arrays split into blocks. Blocks are ordered by a key (e.g., max per block); inside each block elements are unordered. Locate block via index, then sequentially scan inside.
Click "Step forward" to watch the algorithm state change.
- Large sequential tables
- Frequent lookups, few inserts/deletes
- Split into blocks of size (last may be smaller).
- Build index table storing max (or min) key per block.
- Search: locate block via index, then scan inside.
Pseudo-code:
for i = 0 to m-1:
if x <= index[i]:
// sequential search in block i
for j = block[i].start to block[i].end:
if A[j] == x:
return j
break
return -1
// Block search: index[] is the index table (max key per block),
// and each block covers the range [block * blockSize, block * blockSize + blockSize).
int BlockSearch(int A[], int n, int blockSize, int index[], int m, int key) {
int block = -1;
// (1) Locate the block through the index table (sequential search)
for (int i = 0; i < m; i++) {
if (key <= index[i]) {
block = i;
break;
}
}
if (block == -1) return -1; // larger than the max key of every block
// (2) Search sequentially inside the block
int start = block * blockSize;
int end = (start + blockSize < n) ? start + blockSize : n;
for (int j = start; j < end; j++) {
if (A[j] == key) return j;
}
return -1; // not found
}
Suppose a table of length is split into blocks, each of length . The blocks are connected by an index that is searched sequentially, and each block is also searched sequentially.
The minimum is reached when , that is, when the number of blocks and the block length are both :
This is exactly why block search reaches .
- Build index:
- Search: (when block count and block size are both )
Exercise 1
Briefly state block search idea and scenarios.
Approach: Locate the block through the index first, then search inside the block sequentially.
Steps:
- Split the table into blocks, search sequentially inside a block, and use an index between blocks
- Suits large tables with frequent lookups and few updates
Answer: Blocking plus an index, which suits large tables.
Exercise 2
100 elements, 10 blocks of 10. Max comparisons?
Approach: The index plus the search inside the block.
Steps:
- The index lookup takes at most 10 comparisons
- The search inside the block takes at most 10 comparisons
- 20 comparisons in total
Answer: At most 20 comparisons.
2018·408
What are the two steps of the block search process, and what is its efficiency?
Approach: Locate the block through the index, then search sequentially inside it; the efficiency is .
Steps:
- Search the index table to locate the block
- Search sequentially inside the block
- Search efficiency
Answer: Two steps, with efficiency .
