AoA Part3 Notes
AoA Part3 Notes
AoA Part3 Notes
Big O notation characterizes functions according to their growth rates: different functions
with the same growth rate may be represented using the same O notation. The letter O is used
because the growth rate of a function is also referred to as order of the function. A description
of a function in terms of big O notation usually only provides an upper bound on the growth
rate of the function. Associated with big O notation are several related notations, using the
symbols o, Ω, ω, and Θ, to describe other kinds of bounds on asymptotic growth rates.
big-O notation
Formal Definition: f(n) = O(g(n)) means there are positive constants c and k, such that
0 ≤ f(n) ≤ cg(n) for all n ≥ k. The values of c and k must be fixed for the function f and must
not depend on n.
Also known as O.
Note: As an example, n² + 3n + 4 is O(n²), since n² + 3n + 4 < 2n² for all n > 10 (and many
smaller values of n). Strictly speaking, 3n + 4 is O(n²), too, but big-O notation is often
misused to mean "equal to" rather than "less than". The notion of "equal to" is expressed by
Θ(n).
The importance of this measure can be seen in trying to decide whether an algorithm is
adequate, but may just need a better implementation, or the algorithm will always be too
slow on a big enough input. For instance, quicksort, which is O(n log n) on average, running
on a small desktop computer can beat bubble sort, which is O(n²), running on a
supercomputer if there are a lot of numbers to sort. To sort 1,000,000 numbers, the quicksort
takes 20,000,000 steps on average, while the bubble sort takes 1,000,000,000,000 steps! See
Jon Bentley, Programming Pearls: Algorithm Design Techniques, CACM, 27(9):868,
September 1984 for an example of a microcomputer running BASIC beating a supercomputer
running FORTRAN.
An Example:
• If an algorithm takes 1 second to run with the problem size 8, what is the time
requirement (approximately) for that algorithm with the problem size 16?
• If its order is:
O(1) è T(n) = 1 second
O(log2n) è T(n) = (1*log216) / log28 = 4/3 seconds
O(n) è T(n) = (1*16) / 8 = 2 seconds
O(n*log2n) è T(n) = (1*16*log216) / 8*log28 = 8/3 seconds
O(n2) è T(n) = (1*162) / 82 = 4 seconds
O(n3) è T(n) = (1*163) / 83 = 8 seconds
O(2n) è T(n) = (1*216) / 28 = 28 seconds = 256 seconds
Properties of Growth-Rate Functions