






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An analysis of the array max algorithm in python, explaining its purpose, consumption, production, and operation counts. It also includes big-o notation analysis and proof of induction. Both recursive and non-recursive approaches.
Typology: Lecture notes
1 / 30
This page cannot be seen from the preview
Don't miss anything!























array max - python - non-recursive
What is the slope of the line: T( n ) = 5 + 4 * ( n – 1) A. ¼ B. 4/ C. 2 D. 4 E. 5
#!/usr/bin/python def array_max(n, array): """array_max: num * num[pos] -> num Purpose: Finds the maximum value of the first n elements in an array Consumes: a positive number n and an array of numbers with at least n elements Produces: a number Example: array_max(4, [75, 4, 46, 12]) -> 75 """ if n == 1: # if we are looking for the max of 1
return array[0] # return that element else: # otherwise, return the max of the last element
return max(array[n-1], array_max(n - 1, array)) array max - python - recursive
array max - python - recursive - op counts
def array_max(n, array) : if n == 1: 1 op: comparison return array[0] 1 op: value return else: 0 ops: comparison already done return max(array[n-1], array_max(n - 1, array)) 4 ops: value return, math (max), function call, math (-) + A(n-1), the number of ops for array_max(n - 1, array)
What is the slope of the line: A( n ) = 5 * ( n - 1) + 2 A. ¼ B. 4/ C. 2 D. 4 E. 5
What is the slope of the line: A( n ) = 5 * ( n - 1) + 2 A. ¼ B. 4/ C. 2 D. 4 E. 5 5n – 3 O(n) recursive 4n + 1 O(n) iterative
Big O
Big O