Our teacher gave us the following definition of Big O notation:

O(f(n)): A function g(n) is in O(f(n)) (“big O of f(n)”) if there exist constants c > 0 and N such that |g(n)| ≤ c |f(n)| for all n > N. I'm trying to tease apart the various components of this definition. First of all, I'm confused by what it means for g(n) to be in O(f(n)). What does in mean?

Next, I'm confused by the overall second portion of the statement. Why does saying that the absolute value of g(n) less than or equal f(n) for all n > N mean anything about Big O Notation?

My general intuition for what Big O Notation means is that it is a way to describe the runtime of an algorithm. For example, if bubble sort runs in O(n^2) in the worst case, this means that it takes the time of n^2 operations (in this case comparisons) to complete the algorithm. I don't see how this intuition follows from the above definition.

  • 1
    $\begingroup$ Welcome to Computer Science! Note that you can use LaTeX here to typeset mathematics in a more readable way. See here for a short introduction. $\endgroup$ – FrankW Oct 28 '14 at 5:44
  • $\begingroup$ It doesn't, because it's wrong. See also our reference questions. $\endgroup$ – Raphael Oct 28 '14 at 7:37
  • $\begingroup$ I'm closing this as a duplicate because I think that the existing question sufficiently addresses your misconception. As a general rule, explaining definitions on this level is not something that SE is good at. You seem to have problems at parsing quite basic mathematical formula, and you seem to be mistaking your (instructor's?) intuition for a definition the formal one has to agree with. Therefore, this is a problem you should approach your teacher(s) with. $\endgroup$ – Raphael Oct 28 '14 at 7:39

O(f(n)) means the family of function of f(n), a family of function for example could be {n^2, 3*n^2, 6*n^2, ...k*n^2}, for g(n) to be in the O(f(n)), it means that g(n) is less than all elements of f(n) in the long run. In the above example, it could be g(n) = n, versus O(f(n)) = O(n^2), obviously, n^2 is bigger than n for all n in the long run.

Absolute value sign compares the absolute values of these two functions so that even if g(n) is negative, we can compare which negative grows faster i.e. -n^2 grows faster than -n is equivalent to n^2 grows faster than n

n>N means in the long run

Bubble sort runs O(n^2) in the worst case means that Bubble sort can run much faster than O(n^2), but it does not exceed O(n^2). For example, we want to sort two elements {2,1} using bubble sort, obviously this takes 1 operation and that's it, 1 is smaller than n^2.

| cite | improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.