Data Structures19 sections · 729 units
Open in Course

Vocabulary - Block Size

Choosing B

The block size BB determines your time complexity. For most problems, B=⌊n⌋B = \lfloor\sqrt{n}\rfloor is optimal. You'll have ⌈n/B⌉\lceil n/B \rceil blocks.

The last block might be smaller than BB if nn isn't perfectly divisible. Given index ii, its block number is ⌊i/B⌋\lfloor i/B \rfloor.

Elements ii and jj are in the same block if ⌊i/B⌋=⌊j/B⌋\lfloor i/B \rfloor = \lfloor j/B \rfloor. Block kk covers indices from k⋅Bk \cdot B to min⁡((k+1)⋅B−1,n−1)\min((k+1) \cdot B - 1, n-1).

This indexing math is the foundation. Every sqrt decomposition algorithm uses it.