subject

Question 1: You are given 2 implementations for a recursive algorithm that calculates the sum of all the elements in a list (of integers): def sum_lst1(lst): if (len(lst) == 1): return lst[0] else: rest = sum_lst1(lst[1:]) sum = lst[0] + rest return sum def sum_lst2(lst, low, high): if (low == high): return lst[low] else: rest = sum_lst2(lst, low + 1, high) sum = lst[low] + rest return sum Note: The implementations differ in the parameters we pass to these functions: In the first version we pass only the list (all the elements in the list have to be taken in to account for the result). In the second version, in addition to the list, we pass two indices: low and high (low ≤ high), which indicate the range of indices of the elements that should to be considered. The initial values (for the first call) passed to low and high would represent the range of the entire list.1) Make sure you understand the recursive idea of each implementation. 2) Analyze the running time of the implementations above. For each version: i) Draw the recursion tree that represents the execution process of the function, and the local-cost of each call. ii) Conclude the total (asymptotic running time of the function. 3) Which version is asymptotically faster?

ansver
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 01:00, zuleromanos
)a grad student comes up with the following algorithm to sort an array a[1..n] that works by first sorting the first 2/3rds of the array, then sorting the last 2/3rds of the (resulting) array, and finally sorting the first 2/3rds of the new array. 1: function g-sort(a, n) . takes as input an array of n numbers, a[1..n] 2: g-sort-recurse(a, 1, n) 3: end function 4: function g-sort-recurse(a, `, u) 5: if u ⒠` ≤ 0 then 6: return . 1 or fewer elements already sorted 7: else if u ⒠` = 1 then . 2 elements 8: if a[u] < a[`] then . swap values 9: temp ↠a[u] 10: a[u] ↠a[`] 11: a[`] ↠temp 12: end if 13: else . 3 or more elements 14: size ↠u ⒠` + 1 15: twothirds ↠d(2 ◠size)/3e 16: g-sort-recurse(a, `, ` + twothirds ⒠1) 17: g-sort-recurse(a, u ⒠twothirds + 1, u) 18: g-sort-recurse(a, `, ` + twothirds ⒠1) 19: end if 20: end function first (5 pts), prove that the algorithm correctly sorts the numbers in the array (in increasing order). after showing that it correctly sorts 1 and 2 element intervals, you may make the (incorrect) assumption that the number of elements being passed to g-sort-recurse is always a multiple of 3 to simplify the notation (and drop the floors/ceilings).
Answers: 3
image
Computers and Technology, 22.06.2019 21:30, mima851
Elements such as fonts colors visual structure graphics and the interface of a web page should complement each other to ensure blank
Answers: 3
image
Computers and Technology, 24.06.2019 10:20, savyblue1724707
Identify the publisher in this citation: carter, alan. a guide to entrepreneurship. new york: river’2008.print.
Answers: 3
image
Computers and Technology, 25.06.2019 12:00, Naysa150724
Matching 1. many steps descending into a solution 2. the technological process known for its high degree of precision 3. method that allows developers freedom when they are writing software a.)six sigma b.)agile software development organization c.)waterfall method
Answers: 1
You know the right answer?
Question 1: You are given 2 implementations for a recursive algorithm that calculates the sum of all...

Questions in other subjects:

Konu
Physics, 18.02.2021 18:00
Konu
English, 18.02.2021 18:00
Konu
Mathematics, 18.02.2021 18:00