

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


10,000 20,000 30,000 40,000 50,000 60,
10 sec
30 sec
50 sec
20 sec
40 sec
60 sec
n
Execution Time
import time def main(): n = eval(input("Enter size of list: ")) aList = list(range(1, n+1)) start = time.clock() sum = sumSomeListItems(aList) end = time.clock() print("Time to sum the list was %.9f seconds" % (end-start)) def sumSomeListItems(myList): """Returns the sum of some items in myList""" total = 0 index = len(myList) - 1 while index > 0: total = total + myList[index] index = index // 2 return total main()
a) What is the problem size of sumSomeListItems?
b) If we input n of 10,000 and sumSomeListItems takes 10 seconds, how long would you expect sumSomeListItems to take for n of 20,000?
(Hint: For n of 20,000, how many more times would the loop execute than for n of 10,000?)
c) What is the big-oh notation for sumSomeListItems?
d) Add the execution-time graph for sumSomeListItems to the graph.
Lecture 3 Page 1
i = 1 while i <= n: for j in range(n):
i = i * 2
a) Analyze the above algorithm to determine its big-oh notation, O ( ).
b) If n of 10,000, takes 10 seconds, how long would you expect the above code to take for n of 20,000?
c) Add the execution-time graph for the above code to the graph.
a) Any array element can be accessed randomly by calculating its address. For example, address of X[5] = 4000 + 5 * 4 = 4020. What is the general formula for calculating the address of the ith element in an array?
b) What is the big-oh notation for accessing the ith element?
c) A Python list uses an array of references (pointers) to list items in their implementation of a list. For example, a list of strings containing the alphabet:
'a' 'b' 'c' 'z'
0 1 2 3 (len( )-1)
Since a Python list can contain heterogeneous data, how does storing references in the list aid implementation?
Lecture 3 Page 2
Execution flow i = 1 (^) i = 2 i = 4 (^) i = n
j = 0 to n-1 j = 0 to n-1^ j = 0 to n- loops n times loops n times loops n times
address
Memory
X[0] X[1] X[2] X[3] X[4] X[5] X[6]