Lab-03-solutions
Lab-03-solutions
Lab 3
Student name: Answer Key
(b) What is the asymptotic growth rate (i.e., Big O ) of the algorithm?
For the full analysis, use all basic operations as discussed in class. You’ll have to think
about what causes the function to run longer: sometimes, you’ll want use the length of
the ArrayList as the input size n. Assume any additional function calls (like isFull())
cost 1 operation.
Function 1
T (n) = 1 + 1 =2
isFull throw
T (n) = 1 +1+ 2 = 4
isFull = ++
1
CS 20, Spring 2025 – Lab 3 2
Function 2
Use only the length of from as the input size n. For this exercise, assume the other
ArrayList member functions cost 1 operation. (For append’s cost, you could also use
your result from Function 1. Does it make a difference in your answer?)
1 template < typename T >
2 void copy ( const ArrayList <T > & from , ArrayList <T > & to ) {
3 int n = from .getLength() ;
4 if ( n > to .getMaxSize() ) {
5 throw std :: string ( " error : not enough space to copy " ) ;
6 }
7
8 to .clear() ;
9 for ( int i = 0; i < n ; i++) {
10 to .append( from .getElement( i ) ) ;
11 }
12 }
T (n) = 1 + 1 +1+ 1 + 1 =5
= getLength > getMaxSize throw
Note that last < appears on the far right of the equation because the for-loop checks
its condition one last time in order to exit (when the condition is false).
Case 2 dominates, and thus T (n) = O(n), where n is the length of the ArrayList<T> from.
Answer Key
CS 20, Spring 2025 – Lab 3 3
Function 3
T (n) = 1 + 1 + 1 + 1 =4
< || >= throw
T (n) = 1| +{z
1 + 1} +1 = 4
=
as before
Answer Key
CS 20, Spring 2025 – Lab 3 4
Function 4
In the scenario where the given position is valid, you’ll need two variables, one for the
length and one for the position; or, for a simpler exercise, start by assuming that the
position is 0.
1 template < typename T >
2 void ArrayList <T >:: remove ( int position ) {
3 if ( position < 0 || position >= this - > length ) {
4 throw std :: string ( " remove error : position out of bounds " ) ;
5 }
6
7 for ( int i = position ; i < this - > length - 1; i++) {
8 buffer [ i ] = buffer [ i + 1];
9 }
10 this - > length--;
11 }
T (n, m) = 1| + 1 {z
+ 1 + 1} = 4
as before
Case 2: position is valid. If we assume that position = 0, then the math looks like
the loop runs n−1 times loop check
z }| { z }| {
1 + 1} +1 + (n − 1)(1 + 1 + 1 + 1 + 2 ) + 1 + 1 + 2 = 6n + 2
T (n, m) = 1| +{z
= < - = + ++ < - --
as before
If we do not assume so, all that changes is the number of times the loop runs: that
number is exactly length − 1 − position, or n − m − 1, and we instead get
Note that if position is valid, then it must be true that n − m ≥ 0. Case 2 dominates;
T (n, m) = O(n − m), where n = length and m = position. This is the best answer for
this analysis.
If you assumed position = 0 you would instead get the simpler, less-precise answer:
T (n) = O(n), where n = length.
Answer Key
CS 20, Spring 2025 – Lab 3 5
Function 5
In the scenario where the given position is valid and the buffer is not full, you’ll need
two variables, one for the length and one for the position; or, for a simpler exercise, start
by assuming that the position is 0.
1 template < typename T >
2 void ArrayList <T >:: insert ( int position , const T & elem ) {
3 if (isFull() ) {
4 throw std :: string ( " insert error : arraylist is full " ) ;
5 }
6 if ( position < 0 || position > this - > length ) {
7 throw std :: string ( " insert error : position out of bounds " ) ;
8 }
9
10 for ( int i = this - > length ; i > position ; i--) {
11 buffer [ i ] = buffer [ i - 1];
12 }
13
Case 3: isFull is false, and position is valid. If we assume that position = 0, then the
math looks like
the loop runs n times
z }| {
+ 1 + 1} +1 + n(1 + 1 + 1 + 2 ) +1 + 1 + 2 = 5n + 9
T (n, m) = 1| + 1 {z
= > = - -- > = ++
as before
If we do not assume so, all that changes is the number of times the loop runs: that
number is exactly length − position, or n − m, and we instead get
T (n, m) = 5 + (n − m)(5) + 4 = 5(n − m) + 9
If you assumed position = 0 you would instead get the simpler, less-precise answer:
T (n) = O(n), where n = length.
Answer Key