0% found this document useful (0 votes)
4 views

POGIL-07-AlgorithmAnalysis-fillable Complete

The document introduces algorithm analysis with a focus on efficiency and Big O notation to describe running times of code. It includes examples of methods with constant time O(1), linear time O(n), and quadratic time O(n^2), along with team roles for a collaborative activity. Additionally, it discusses searching algorithms, comparing linear and binary search in terms of efficiency.

Uploaded by

garrettfoltyn
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)
4 views

POGIL-07-AlgorithmAnalysis-fillable Complete

The document introduces algorithm analysis with a focus on efficiency and Big O notation to describe running times of code. It includes examples of methods with constant time O(1), linear time O(n), and quadratic time O(n^2), along with team roles for a collaborative activity. Additionally, it discusses searching algorithms, comparing linear and binary search in terms of efficiency.

Uploaded by

garrettfoltyn
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/ 10

Algorithm Analysis

One aspect of computer science is related to efficiency. How long does it take a program to process
the data and return results? Up to now, we have focused on writing “correct” programs – programs
that process data and return results according to a specification. Moving forward in this course, we
will also focus on efficiency, how fast the program is in completing its task. In general, faster
programs are preferred.
This activity’s purpose is to introduce Big O notation for describing the running time for sections of
code. This notation allows us to compare functions to one another along the dimension of time and
gives us a common language for communicating the running time of code segments.

Before you start, complete the form below to assign a role to each member. If you have 3 people,
combine Speaker & Reflector.

Team Roles Team Member

Recorder: records all answers & questions, Garrett


and provides copies to team &
facilitator.
Presenter: talks to facilitator and other
teams.
Manager: keeps track of time and makes sure Aidan
everyone contributes
appropriately.
Reflector: considers how the team could
work and learn more effectively. Armaan
(10 min) I. Model 1: reorder method start
time: 12:49
// This method sorts two integer array elements so the smaller element is
// placed in the first index (i) and the larger in the second index (j)
public void reorder(int[] array, int i, int j) {
if (array[i] > array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

1. Suppose an array, arr, contains the values 6, 11, 9, 13. List the contents of arr after the method
call reorder(arr, 1, 2).
Before After

6 11 9 13 6 9 11 13
2. Suppose we define an “operation” as an assignment statement, arithmetic operation, or comparison.

How many operations does the method execute when reorder(arr, 1, 2) is called?

3. How many operations does the method execute when reorder(arr, 0, 1) is called?

4. Suppose an array, bArr contains the values 2, 6, 13, 8, 3, 7. How many operations does the method
execute when reorder(bArr, 3, 4) is called?
4
Before After

2 6 13 8 3 7 18 2 6 13 3 8 7 18
5. How many operations does the method execute when
reorder(bArr, 1, 2) is called? 1

6. Is there a maximum number of operations that reorder can execute? Why or why not?
Justify your answer in complete sentences and be prepared to report out to the entire class.
Yes, because in the code there are only 4 operations that can be performed

7. Does the number of operations the method executes depend on the size of its input (i.e., the number
of elements in the input)? Why or why not?
Justify your answer in complete sentences and be prepared to report out to the entire class
No because it only ever accesses two items within an array

8. We say that the reorder method executes in constant time. Another way to say this is that the
method is O(1). Complete the following sentence:

A method is O(1) (or executes in constant time) if...


the amount of time to run the method does not
change between operations

End time: 1255


(10 min) II. Model 2: normalize method start
time: 1255
// This method maps values in the range [min…max] to the range [0…1]
public void normalize (double [] array, double min, double max) {
for (int i = 0; i < array.length; i ++) {
array[i] = (array[i] − min) / (max - min);
}
}

9. Suppose an array a contains the values 5, 15, 10 and normalize the method is called with the
following parameter values: normalize(a, 5, 15);
What are the contents of the array after this method call?

Before After

5 15 10 0 1 0.5

10. How many operations does the method execute when normalize(a, 5, 15) is called?
Note: the initialization of the variable i executes once, before the first iteration of the loop. The
iteration and comparison statements occur after each iteration of the loop.

Arithmetic
Operation Assignment Comparison
Operation

How many times? 4 13 4


11. Suppose the normalize method is called with an array of length 20 as an argument. How many
operations are executed by the method?
103

12. Suppose the normalize method is called with an array of length n as an argument. How many
operations are executed by the method?
2(n+1)+(4n+1)
13. We say that the normalize method runs in linear time. Another way to say this is that the method
is O(n), read as ‘Big Oh of N’. Based on your team’s observations about the number of operations in
the normalize method, as a team complete the following sentence:
Justify your answer in complete sentences and be prepared to report out to the entire class
A method is O(n) (or executes in linear time) if...

the time to take to run the method increases 1 to 1 with the size of the input

14. We say that quadratic time methods are O(n2). Based on your team’s definition of O(n), complete
the following sentence:

Justify your answer in complete sentences and be prepared to report out to the entire class
A method is O(n2) (or executes in quadratic time) if...

the time to run the method increases exponentally with the size of the input

End time: 1:03


(15 min) III. Practice start
time: 1:03
For each of the following methods, describe what the method does and label it either O(1), O(n), or O(n2).

Justify your answers in complete sentences and be prepared to report out to the entire class.

15. The max method is O( ).


public int max(int a, int b) {
int result;
if (a > b) {
result = a;
}
else {
result = b;
}
return result;
}

The max method is O(1).

16. The maxElement method is O( ).


public int maxElement (int[ ] array) {
int max = array[0];

for (int i = 0; i < array.length; i++){


if (array[i] > max) {
max = array[i];
}
}
return max;
}

maxElement method is O(n)


17. The maxSubseqSum method is O( ).
public int maxSubseqSum (int[ ] array){
int max = array[0];

for(int i = 0; i < array.length; i++){


int sum = 0;
for(int j = i; j < array.length; j++){
sum += j;

if (sum > max) {


max = sum;
}
}
}
return max;
}

O(n^2)

End time: 105


(10 min) IV Considering how we search start
time: 106
18. What searches do you conduct? (in the physical world or with software)?

Search the libary, search on google, and browse social media

19. One specific example of searching for data is looking for an item in a sorted list. Your lab TAs do this
regularly during lab – looking for your name on the roster so they can view your code in Gradescope.
Can you think of a collection of data that you use that is sorted in some way? What is it?

Contact - they are sorted aphibetically

If we have data that is sorted, searching for that data can be made more efficient by using a technique
called binary search.
Assume the data is stored in sorted order in an array. Assume you are looking for a particular item,
called key.
• Maintain two indices in the array that get closer and closer together to “zero in” on the key.
• The indices start out at positions 0 and the length of the array minus 1.
• You calculate the midpoint of the indices and look at the array element at that midpoint.
• That value tells you to search to the right of that midpoint or to the left of that midpoint.
• Eventually, the indices will be equal or cross over each other.
• At that point, the key is found or not found.
Examine the binary_search method on the attached binary search code sheet and answer the
following questions.
20. Suppose the user types 67 when this program is executed. Trace the recursive calls for
binary_search? Keep going until recursion stops.

binary_search(67, test_array, 0, 10) first_index last_index

binary_search(67, test_array, , ) 0 10

binary_search(67, test_array, , ) 5 10
8 10
binary_search(67, test_array, , )
9 10
binary_search(67, test_array, , ) 10 10
21. In the above execution, what are the successive values of mid that are assigned as the function makes
recursive calls?
mid = 5 mid

mid = 5
8
mid =
9
mid =
10
22. Suppose the user types 18. What are the recursive calls for binary_search? Keep going until recursion
stops.
binary_search(18, test_array, 0, 10)
first_index last_index
binary_search(18, test_array, , ) 0 10
binary_search(18, test_array, , ) 5 10
binary_search(18, test_array, , ) 8 10
9 10
binary_search(18, test_array, , )
10 10

Look at the linear_search method in the provided code and answer the following questions.

23. Assume linear_search is called on (67, test_array, 11). How many numbers in
test_array are examined before the function returns?

11

24. How many recursive calls were made when using binary search to find 67?
5

25. Which function is faster, in general, for searching? binary search or linear search? Justify your
answer in complete sentences and be prepared to report out to the entire class

Binary for a sorted because it cut the list in half with each search.
Big-O running time analysis is a mathematical function that is based on the input size N of the data
for the program. In these searching algorithms, N is the size of the array.
Think about how many operations are performed in linear search and binary search given an array
of N items. In other words, how many elements are examined in linear search for an array of length
N; how many function calls binary search makes for an array of length N, since each function
execution is a constant amount of code.
Now, answer the following question about linear search and binary search.

26. What is the big-O running time of linear search if there are N elements in the array?

27. What is the big-O running time of binary search if there are N elements in the array?

log 2 of n

End time: 1:17

Remember to save your pdf. When you upload it to Gradescope remember to add your teammates as
Group Member to your submission so everyone earns points for this activity.

Complete the Reflector’s Report as a Team.

You might also like