Sie sind auf Seite 1von 10

Exercises: Part I

Author: Mala Mitra


Question 0
• (A) What is the number of steps it will take to
run Program 1 in the best case and worst
case? Express your answer in terms of n, the
size of the input x. Consider only variable
assignments.

• Best case x=0, the second loop will not work


• Worst case when x is very large (inf)
Question 0 contd.
• def program1(x):
– total = 0
– for i in range(1000):
• total += i
– while x > 0:
• x -= 1
• total += x
– return total
Question 1
• Recall that we write f(n)=O(g(n)) to express
the fact that f(n) grows no faster than g(n):
there exist constants N and c>0 so that for
all n≥N, f(n)≤c⋅g(n).
• Is it true that log2n=O(n2)?
• No, log2n<O(n2)?
Question 2
• True / False?
• nlog2n=O(n)

• False (nlogn > n)


Question 3
• n2 = O(n3)
• True / False?

• False (n3 > n2)


Question 4
• n=O(√n)
• True / False?

• False (n > √n)


Question 5
• 5log2n = O(n2)
• True / False?

• False (n2 > 5log2n )


Question 6
• n5 = O(23log2n)
• True / False?

• False (23log2n > n5 )


Question 7
• 2n = O(2n+1)
• True / False?
• I am not sure
• False (2n+1> 2n )

Das könnte Ihnen auch gefallen