B2.4.1 · Efficiency and memory

Auxiliary space

Python · step by step

Auxiliary space

Compare a running total with a growing copy of the input.

Select Play or Next line to follow Python execution.

Python implementation

The highlight marks the next line to execute. Values show the state before that line.

def auxiliary_space(items, copy_input):    total = 0    working = []    for value in items:        total = total + value        if copy_input:            working.append(value)    return total

The supplied list is input space. total and value occupy a fixed number of slots in our classroom model. The copy grows to n elements. Without the copy: O(1) auxiliary space; with it: O(n). Both scans take O(n) time.

Keep exploring

Return to the algorithm efficiency lesson →

All B2.4 animations →

All practice & resources →