POGIL-07-AlgorithmAnalysis-fillable Complete
POGIL-07-AlgorithmAnalysis-fillable Complete
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.
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:
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
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
Justify your answers in complete sentences and be prepared to report out to the entire class.
O(n^2)
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?
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
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
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.