Ahmadmj 3
Ahmadmj 3
Formula indicating order of growth with specific multiplicative constant e.g., C(n) ≈ 0.5 n2
Formula indicating order of growth with unknown multiplicative constant e.g., C(n) ≈ cn2
Order of growth
How much faster will algorithm run on computer that is twice as fast?
How much longer does it take to solve problem of double input size?
A way of comparing functions that ignores constant factors and small input sizes
Big-omega
Big-theta
Definition: f(n) is in O(g(n)) if order of growth of f(n) ≤ order of growth of g(n) (within constant
multiple),
i.e., there exist positive constant c and non-negative integer n0 such that
Examples:
n is O(n2)
100n+5 is O(n2)
Definition: f(n) is in Ω(g(n)) if order of growth of f(n) ≥ order of growth of g(n) (within constant
multiple),
i.e., there exist positive constant c and non-negative integer n0 such that
Examples:
n3 is Ω(n2)
Definition: A function f(n) is in Θ(g(n)) if order of growth of f(n) is bounded both above and below by
some positive constant of g(n) for all large n, i.e., there exist some positive constant c1 and c2 and
non-negative integer n0 such that
Examples:
0.5n(n-1) is Θ(n2)
f(n) O(f(n))
Examples:
10n vs. n2
n(n+1)/2 vs. n2
10n vs. n2
Therefore, the first function 10n has smaller order of growth than n2
n(n+1)/2 vs. n2
n2 2 n2
Therefore the first function n(n+1) has the same order of growth with n2
b n d(n) 1
Orders of growth of some important functions
All polynomials of the same degree k belong to the same class: aknk + ak-1nk-1 + … + a0 (nk)
order log n < order n (>0) < order an < order n! < order nn
1 constant
log n logarithmic
n linear
n log n n-log-n
n2 quadratic
n3 cubic
2n exponential
n! factorial
Set up a sum for the number of times the basic operation is executed
liu1 = 1+1+…+1 = u - l + 1
The obvious measure of an input’s size here is the number of elements in the array, i.e. n
The operations that are going to be executed most often are in the algorithm’s for loop. The
basic operation is the comparison:
Since the number of comparisons will be the same for all arrays of size n, there is no need to
distinguish among the worst, average and best cases here.
Let C(n) denote the number of times this comparison is executed and try to find a formula
expressing it as a function of size n.
The algorithm makes 1 comparison on each execution of the loop, which is repeated for each
value of the loop’s variable I within the bound 1and n-1 (inclusively).
In particular,
C(n) = 1in-1 1
= (n – 1) -1 + 1
= n-1 (n) .
The algorithm’s basic operation is comparison between A[i] and A[j] which appeared in the inner
double loop
The best-case efficiency of the algorithm is when the first two elements are the same.
Cworst(n) = n2 [ (n-1) - 0 + 1 ]