Sub topic 3
Why indexing is O(1)
This chapter assumes you know basic programming (variables, loops, functions), introductory Big-O notation, and that memory is a sequence of numbered byte locations called addresses. It builds on the previous chapter, where an array was laid out as one contiguous block of memory.
The address formula
An array stores its elements back to back. Every element has the same size, so the position of any element can be computed with one formula:
address of element i = base address + i * element sizeThe base address is where element 0 starts. The element size is the number of bytes each element takes. If an array of 4-byte integers starts at address 1000, then element 3 starts at 1000 + 3 * 4 = 1012.
The computer does not walk past elements 0, 1 and 2 to reach element 3. It does one multiplication and one addition, then reads the memory at that address.
Why the cost is constant
The formula uses the same two arithmetic operations whether the array holds 10 elements or 10 million. The work does not depend on the array's length n, and that is what O(1) means. Reading a[0] and reading a[999999] cost the same number of steps.
Compare this with a structure where you must follow links from one element to the next. Reaching element i there takes i steps, which is O(n). Arrays avoid that because the layout is predictable.
Seeing it in code
The following Python sketch imitates the formula. It uses a plain list of numbers to stand in for memory, where each slot is one byte.
memory = [0] * 2000 # pretend memory, one slot per byte
base = 1000 # where the array starts
size = 4 # bytes per element
def address(i):
return base + i * size
for i in range(3):
print(i, address(i))This prints:
0 1000
1 1004
2 1008Each call does the same amount of work for any i. That is the whole reason indexing is O(1).
What the constant-time claim depends on
Two conditions make the formula work:
- The elements are contiguous, with no gaps between them.
- Every element occupies the same number of bytes.
If either condition fails, the formula breaks. This is why a Python list, whose elements can be objects of different sizes, stores references (fixed-size addresses of the objects) instead of the objects themselves. The list is an array of references, so the formula still works. Reading the object a reference points to is then one extra step, which is still constant.
Writing also costs O(1)
The same address works for assignment. a[i] = value computes the address and stores the value there. Reading and overwriting an element are both constant time. Changing the number of elements is a different operation, and the chapters on shifting and resizing cover it.
Exercise
An array of 8-byte values starts at address 5000. At what address does element 6 start, and how does the cost of finding it change if the array has a million elements instead of ten?
Show answer
The address is 5000 + 6 * 8 = 5048. The cost does not change. It is still one multiplication and one addition, so it is O(1) regardless of the array's length.