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.

Block search

Click "Step forward" to watch the algorithm state change.

When to use

  • Large sequential tables
  • Frequent lookups, few inserts/deletes

Algorithm

  1. Split into mm blocks of size kk (last may be smaller).
  2. Build index table storing max (or min) key per block.
  3. 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

Code Implementation

// 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
}

Deriving the Average Search Length (ASL)

Suppose a table of length nn is split into mm blocks, each of length s=⌈n/m⌉s = \lceil n/m \rceil. The blocks are connected by an index that is searched sequentially, and each block is also searched sequentially.

ASL=ASLindex+ASLblock=m+12+s+12ASL = ASL_{\text{index}} + ASL_{\text{block}} = \frac{m+1}{2} + \frac{s+1}{2}

The minimum is reached when m≈s≈nm \approx s \approx \sqrt{n}, that is, when the number of blocks and the block length are both n\sqrt{n}:

ASLmin=n+1ASL_{min} = \sqrt{n} + 1

This is exactly why block search reaches O(n)O(\sqrt{n}).

Complexity

  • Build index: O(n)O(n)
  • Search: O(n)O(\sqrt{n}) (when block count and block size are both n\sqrt{n})

Exercises

Exercise 1

Briefly state block search idea and scenarios.

Block SearchIndex TableSequential Search

Approach: Locate the block through the index first, then search inside the block sequentially.

Steps:

  1. Split the table into blocks, search sequentially inside a block, and use an index between blocks
  2. 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?

Block SearchIndex TableComparison Count

Approach: The index plus the search inside the block.

Steps:

  1. The index lookup takes at most 10 comparisons
  2. The search inside the block takes at most 10 comparisons
  3. 20 comparisons in total

Answer: At most 20 comparisons.

Past exam

2018·408

What are the two steps of the block search process, and what is its efficiency?

Block SearchSearch EfficiencyTime Complexity

Approach: Locate the block through the index, then search sequentially inside it; the efficiency is n\sqrt{n}.

Steps:

  1. Search the index table to locate the block
  2. Search sequentially inside the block
  3. Search efficiency O(n)O(\sqrt{n})

Answer: Two steps, with efficiency O(n)O(\sqrt{n}).

Found an issue with this content?

Tell us what could be improved.