Lecture 2 - Big O Notation
Lecture 2 - Big O Notation
Hint: Math.random()
ALGORITHM EFFICIENCY : COUNT OPERATIONS
Assume that any of comparison, addition, value setting
and retrieving, etc. is just 1 operation
Examples:
1) 2𝑛3 + 5𝑛2 + 0.15𝑛 + 7 ≈ 𝑛3
2) 2𝑛3 + 3𝑛 ≈ 3𝑛
Big-O notation is a mathematical notation that describes the limiting behavior of a function when the
argument tends towards a particular value or infinity.
BIG-O NOTATION
Law of addition: 𝑂 𝑓 𝑛 +𝑂 𝑔 𝑛 = 𝑂(𝑓 𝑛 + 𝑔(𝑛))
𝑂(𝑛)
Used with sequential statements
𝑂 𝑛 + 𝑂 𝑛2 = 𝑂 𝑛 + 𝑛2 = 𝑂(𝑛2 ) 𝑂(𝑛2 )
𝑂 𝑛 ∗ 𝑂 𝑛 = 𝑂(𝑛2 )
ANALYZING THE RUNNING TIME OF A PROGRAM
COMPLEXITY CLASSES
CONSTANT COMPLEXITY
Complexity independent of inputs
No matter how many operations it performs exactly as long as it
doesn`t depend on the number of inputs
Time complexity is constant