Source1 : http://www.geeksforgeeks.org/analysis-of-algorithms-set-3asymptotic-notations/

Source2: https://www.khanacademy.org/computing/computer-science/algorithms/asymptotic-notation/a/big-o-notation

Big O Notation - Worst case

The Big O notation defines an upper bound of an algorithm, it bounds a function only from above. For example, consider the case of Insertion Sort. It takes linear time in best case and quadratic time in worst case. We can safely say that the time complexity of Insertion sort is O(n^2). Note that O(n^2) also covers linear time.
If we use Θ notation to represent time complexity of Insertion sort, we have to use two statements for best and worst cases:

  1. The worst case time complexity of Insertion Sort is Θ(n^2).
  2. The best case time complexity of Insertion Sort is Θ(n).

The Big O notation is useful when we only have upper bound on time complexity of an algorithm. Many times we easily find an upper bound by simply looking at the algorithm.

f(n) = O(g(n)), f is bounded above by g(up to constant factor) asymptotically.

f(n) <= c * g(n), n >= n0 for c > 0, n0 > 1

Worst case.

Example =>

f(n) = 3n+2

g(n) = n

3n+2 <= c * n

here we can say that for any values greater than c=3, this will be true.

So c=4 and n = 2 can work.

Now as we can see that g(n) = n is is an upper bound for f(n), we can say that n^2, n^3 etc are all upper bounds for f(n). But we always take the tightest upper bound hence g(n) = n.

results matching ""

    No results matching ""