The dynamic array implemented with Cpp with header 'memory'
Amortized Analysis
When dealing with dynamic array data type, we are interested in the
average time cost on each element for a dynamic array of size .
Potential Method
For a data structure , we use
to map
it into a numerical quantity . Then the amortized cost for the manipulation is Intuitively, it manifests that the amortized cost
is the addition of actual cost and the potential variation. Sum the two
sizes up we obtain If one can guarantee
that , then
The Contraction
and Expansion of Dynamic Array
The upperbound for expansion of an array is pretty easy to find.
Assume that we undergo continuous
times of array pushback, then then the average cost is . Consider the contraction
of array, we then apply the potential method. Define as This guarantees that . Popping out an
element, if
and no contraction occurs, then If contraction occurs, then Likewise, when , no contraction
occurs. When and when Hence, we conclude that both the contraction and
expansion manipulation for dynamic array has linear time
consumption.
The
Basic Manipulations Implemented with Build-in Type