CSCI 200
Asymptotic Notation
Definition 1 (Big-O). Given two functions f and g, we say that f (n) = O(g(n)) if there exist positive constants c and n0
such that for all n ≥ n0 , the following inequality holds: f (n) ≤ c · g(n).
Big-O is an intuitive upper-bound. To express a function in Big-O notation, recall the following:
• we omit lower-order terms and constant coefficients.
• nb = o(an ) for all constants a > 1 and b. That is, exponential functions grow faster than polynomial functions.
• logb n = o(na ) for any positive constants a and b. That is, polynomial functions grow faster than polylogarithmic
functions.
Practice Problems - Part I
Assume the following represent the number of instructions an algorithm requires in the worst case. Express its running time
in Big-O notation.
1. 500n2 − 32n + 5
2. n4 + n3 + n
3. 10n + 32 + 2n
4. 10n + 50n log n
5. n2 + n log n
6. n0.2 + log100 n
7. 101000
8. n + n2 log n + n2
1
Practice Problems - Part II
Express the worst-case running time of each of the following functions in Big-O notation.
1. Algorithm Foo ( n ) :
x = 0
for i = 1 . . . n
x = x + 1
2. Algorithm Bar ( n ) :
x = 0
for i = 1 . . . n
for j = i . . . n
x = x + 1
3. Algorithm Cow( n ) :
x = 0
for i = 1 . . . n
for j = 1 . . . 5
x = x + 1
4. Algorithm Cat ( n ) :
x = 0
for i = 1 . . . 5
x = x + 1
5. Algorithm Bird ( n ) :
x = 0
for i = 1 . . . 5
for j = 1 . . . 5
x = x + 1
6. Algorithm Dog ( n ) :
x = n
while x > n :
x = x / 2
7. Algorithm S c o t ( n ) :
x = 1
while x ≤ n :
x = x ∗ 2
8. Algorithm example ( n ) :
return n
2