Question: public class MyArray { / / Assume ints can get arbitrarily large but simple operations / / on them still remain worst case O (
public class MyArray
Assume ints can get arbitrarily large but simple operations
on them still remain worst case O
private int a new int;
private int n m ;
public void addint obj
if n alength
grow;
an obj;
n;
private void grow
Assume array allocation takes O time.
int temp new intm alength ;
m;
for int i ; i n; i
tempi ai;
a temp;
In this problem, you will do an aggregate analysis on the MyArray class under the
assumption that the number of array assignment operations is directly proportional to
the worst case running time of add and grow
a points What is the worstcase running time for one call to add?
Give a tight asymptotic bound.
Justify your answer.
b points Going forward, let T N be the total number of array assignment op
erations in a sequence of N calls to add on a MyArray.
Use your answer from part a to do an overlypessimistic worstcase analysis on
T N State your answer using asymptotic notation.
Justify your answer.
c points Use your overlypessimistic asymptotic upper bound on T N from
part b to derive the amortized cost per call to add on a MyArray. State this
quantity using asymptotic notation.
Justify your answer.
d points Going forward, we will derive a more meaningful upper bound on T N
In a sequence of N calls to add on a MyArray, how many array assignment
operations are executed in JUST add and not grow
Justify your answer.
As part of your justification, state a valid summation expression that represents
this quantity and evaluate it but dont use asymptotic notation.
e points In a sequence of N calls to add an a MyArray, at most how many
array assignment operations are executed in JUST grow
Justify your answer.
As part of your justification, state a valid summation expression that represents the
least upper bound on the number of array assignment operations executed in JUST
grow
You must evaluate upper bound your summation expression but dont use asymp
totic notation.
Hint: Assuming k is a positive integer, then the following is true:
kX
i
i kk k
f points Use your conclusions from parts d and e to derive the amortized
cost per call to add in a sequence of N calls to add on a MyArray.
State your answer using asymptotic notation.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
