Card 01/ 09

ConceptDifficulty: Beginner3 min

Arrays: Contiguous Cells Read by Index

Every cell is the same size and sits next to the last, so any index is one calculation away.

An array stores its elements in one unbroken block of memory, each in a cell of the same size, so the position of any element can be worked out from its index. This card covers that layout, why it makes reading by index O(1), and what it costs to insert, delete or search.

A game keeps a player's last six scores. Asking for the fourth score should not mean walking past the first three, and with an array it does not. Suppose the scores sit in cells that start at memory address 1000, where an address is the number that identifies one byte of memory, and each score takes four bytes. Cell i then starts at 1000 + i × 4. Indexes start at 0 because an index is a distance from the first cell: scores[0] sits at 1000 itself.

Before the trace: at which address does scores[5] start?

At 1020, which is 1000 + 5 × 4.

The trace reads three of the six cells; index marks the cell being read.

Reading three cells of scores by index

Inputscores = 42 17 8 99 23 61, base address 1000, four bytes a cell

  1. Read scores[0]

    1. 42index
    2. 17
    3. 8
    4. 99
    5. 23
    6. 61

    1000 + 0 × 4 = 1000: the first cell sits at the base address.

  2. Read scores[3]

    1. 42
    2. 17
    3. 8
    4. 99index
    5. 23
    6. 61

    1000 + 3 × 4 = 1012: one multiplication and one addition reach 99.

  3. Read scores[5]

    1. 42
    2. 17
    3. 8
    4. 99
    5. 23
    6. 61index

    1000 + 5 × 4 = 1020: the last cell costs the same arithmetic as the first.

ResultEvery read costs one multiplication and one addition, whatever the index.

Reading or writing a cell by its index is O(1) in the worst case, because the arithmetic is the same whatever the index and however long the array. Two properties make it work: every cell is the same size, and the cells are contiguous, each starting where the last one ends. A Python list keeps that layout one step removed: each cell holds a reference, the address of an object stored elsewhere, so the cells stay the same size whatever the objects are.

Books shelved wherever there is room, each with a note saying where the next one is, have neither property. Finding the fourth means following three notes, so the cost of a read grows with its position. That is the shape of a linked list.

The layout also sets the price of every other operation, for an array of n elements whose size was fixed when it was created:

What a fixed-size array's operations cost, with n elements
OperationCostCaseWhy
Read by positionO(1)worstOne multiplication and one addition find the cell
Search by valueO(n)worstA value says nothing about where it is, so every cell may be checked
Insert at index iO(n)worstEvery element from i onwards moves one cell right; at the front, all n move
Delete at index iO(n)worstEvery element after i moves one cell left to close the gap

An array sits underneath many other structures because it finds the element at a position in one step. The price is paid when elements have to move.