Complexity of Algorithms
Complexity of Algorithms
ALGORITHMS
There is rarely a single algorithm that can solve a problem (ie. one
problem can be solved in multiple ways.
Through this, we can infer the best case, average case, and worst-
case scenario of an algorithm.
BIG-O NOTATION
The Big-O Notation is the formal way of describing the upper bound
or worst-case scenario performance of an algorithm.
Big Theta Notation (Θ) describes the tight bound or expected case
of an algorithm.
O, OMEGA, AND THETA AS CONDITIONAL OPERATORS
Big O is like <= i.e., the rate of growth of an algorithm is less than or
equal to a specific value.
Big Omega is like >= i.e., the rate of growth of an algorithm is greater
than or equal to a specific value.
O(1) describes an algorithm that will always execute in the same time (or
space) regardless of the size of the input data set.
x=n
if ( x > 0 ) {
application code
print x
}
O(n) LINEAR
while ( x > 0 ) {
y=x
while ( y > 0 ) {
application code
y=y-1
x=x-1
O(nk) POLYNOMIAL
x=n
while ( x > 0 ) {
x=x/2
}
O(n log n) LINEAR LOGARITHMIC
• Keep the largest term in the function and discard the others
Example:
f(n) = n ((n + 1) / 2)