|
Data Structures and Algorithms
with Object-Oriented Design Patterns in Python |
Program
gives the code
for the Array class __init__ method.
The __init__ method takes three arguments,
self, length and baseIndex.
The length argument gives the desired array length
and the baseIndex argument gives the lower bound for array indices.
The __init__ method creates list of of the desired length
and then sets the _baseIndex.
Note that the default base index is zero
and the default array length is zero.

Program: Array class __init__ method.
In Python, when a list is allocated, two things happen. First, memory is allocated for the list object and its elements. Second, each element of the list is initialized with the appropriate default value (in this case all of the list elements refer to the None object).
For now, we shall assume that the first step takes a constant amount of time.
Since there are
elements to be initialized,
the second step takes O(n) time.
Therefore, the running time of
the Array class __init__ method is O(n).