0% found this document useful (0 votes)
9 views5 pages

Lab-03-solutions

The document is a lab assignment for a CS course that includes multiple functions related to an ArrayList data structure. Each function requires an analysis of the algorithm's performance, including the asymptotic growth rate (Big O notation). The document provides detailed examples and calculations for each function's time complexity based on different cases.

Uploaded by

opbracc578
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Lab-03-solutions

The document is a lab assignment for a CS course that includes multiple functions related to an ArrayList data structure. Each function requires an analysis of the algorithm's performance, including the asymptotic growth rate (Big O notation). The document provides detailed examples and calculations for each function's time complexity based on different cases.

Uploaded by

opbracc578
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

L AS P OSITAS C OLLEGE

Lab 3
Student name: Answer Key

Course: CS 20, Spring 2025


Due date: 2/4/25 @ 8pm

For each function below, answer the following:

(a) What is the full analysis of the algorithm?

(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

1 template < typename T >


2 void ArrayList <T >:: append ( const T & elem ) {
3 if (isFull()) {
4 throw std :: string ( " append error : arraylist is full " ) ;
5 }
6
7 buffer [ this - > length ] = elem ;
8 this - > length++;
9 }

Case 1: isFull() is true

T (n) = 1 + 1 =2
isFull throw

Case 2: isFull() is false

T (n) = 1 +1+ 2 = 4
isFull = ++

Either way, T (n) = O(1).

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 }

Case 1: n > to.getMaxSize() is true

T (n) = 1 + 1 +1+ 1 + 1 =5
= getLength > getMaxSize throw

Case 2: n > to.getMaxSize() is false


the loop runs n times
z }| {
T (n) = 1| + 1 {z
+ 1 + 1} + 1 + 1 + n(1 + 1 + 1 + 2 ) +1 = 5n + 7
clear = < append getElement ++ <
as before

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

1 template < typename T >


2 void ArrayList <T >:: replace ( int position , const T & elem ) {
3 if ( position < 0 || position >= this - > length ) {
4 throw std :: string ( " replace error : position out of bounds " ) ;
5 }
6
7 buffer [ position ] = elem ;
8 }

Case 1: position is not valid

T (n) = 1 + 1 + 1 + 1 =4
< || >= throw

Case 2: position is valid

T (n) = 1| +{z
1 + 1} +1 = 4
=
as before

In both cases, T (n) = 4 and thus T (n) = O(1).

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 }

Let n = length and m = position.

Case 1: position is not valid

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

T (n, m) = 4 + (n − m − 1)(6) + 4 = 6n − 6m + 2 = 6(n − m) + 2

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

14 buffer [ position ] = elem ;


15 this - > length++;
16 }

Let n = length and m = position.


Case 1: isFull is true
T (n, m) = 1 + 1 =2
isFull throw

Case 2: isFull is false, and position is not valid


T (n, m) = 1 + 1| +{z
1 + 1} + 1 = 5
isFull throw
position

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

Again, if position is valid, then it must be true that n − m ≥ 0. Case 3 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

You might also like