Block Search
Idea
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.
分块查找法演示
块1:
371
块2:
958
块3:
121511
When to use
- Large sequential tables
- Frequent lookups, few inserts/deletes
Algorithm
- 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
Complexity
- Build index:
- Search: (when block count and block size are both )
Exercises
Exercise 1
Briefly state block search idea and scenarios.
参考答案
思路:先索引定位块,再块内顺序查。
步骤:
- 分块,块内顺序;块间用索引
- 适合大表、查找频繁、更新少
答案:分块+索引,适合大表。
Exercise 2
100 elements, 10 blocks of 10. Max comparisons?
参考答案
思路:索引 + 块内。
步骤:
- 索引最多 10 次
- 块内最多 10 次
- 总计 20 次
答案:最多 20 次。
Past exam
2018·408
分块查找过程两步?效率?
参考答案
思路:索引定位 + 块内顺序;效率 。
步骤:
- 查索引表定位块
- 块内顺序查
- 查找效率
答案:两步,效率 。