CSE 230: Data Structures: Lecture 2: Complexity Analysis
CSE 230: Data Structures: Lecture 2: Complexity Analysis
● If Else
– Cost of if part of else part whichever is higher
cg(n)
f(n)
n0 n
f(n) = (g(n))
Amrita School of Engineering
CSE 201: Data Structures and
Algorithms Amrita Vishwa Vidyapeetham
Big-Oh Example
● Show 7n-2 is O(n)
– need c > 0 and n0 >= 1 such that 7n-2 <= cn for n >= n0
– this is true for c = 7 and n0 = 1
● Show 3n3 + 20n2 + 5 is O(n3)
– need c > 0 and n0 >= 1 such that 3n3 + 20n2 + 5 <= cn3 for n >= n0
– this is true for c = 4 and n0 = 21
● n2 is not O(n)
– Must prove n2 <= cn
– n <= c
– The above inequality cannot be satisfied since c must be a constant
– Hence proof by contradiction
Amrita School of Engineering
CSE 201: Data Structures and
Algorithms Amrita Vishwa Vidyapeetham
Exercises
● Show that 8n+5 is O(n)
● Show that 20n3 +10nlogn+5 is O(n3)
● Show that 3logn+2 is O(logn).