Math for sorts, and statements about computational constants which are not time

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

This is the only article I’ve read that makes sense of the math for me. I especially like the point that we are not talking about time, when we say stuff like “The BubbleSort has a bad worst case scenario.” We are actually talking about computations, and we are only talking about time implicitly, to the extent that we have in our heads an implicit computational model in which the time to do some action, such as comparing 2 keys, is constant.

In computer science and sometimes mathematics, big-O notation is used
to talk about how quickly a function grows while disregarding
multiplicative and additive constants. When classifying algorithms,
big-O notation is useful because it lets us abstract away the
differences between real computers as just multiplicative and additive
constants.

Big-O is not a difficult concept at all, but it seems to be common
even for people who should know better to misunderstand some aspects of
it. The following is a list of misconceptions that I have seen in the
wild.

But first a definition: We write

f(n)=O(g(n))

when f(n)Mg(n) for sufficiently large n, for some positive constant M.

Misconception 1: “The Equals Sign Means Equality”

The equals sign in

f=O(g(n))

is a widespread travestry. If you take it at face value, you can deduce that since 5n and 3n are both equal to O(n), then 3n must be equal to 5n and so 3=5.

The expression f=O(g(n))
doesn’t type check. The left-hand-side is a function, the
right-hand-side is a … what, exactly? There is no help to be found in
the definition. It just says “we write” without concerning itself with
the fact that what “we write” is total nonsense.

The way to interpret the right-hand side is as a set of functions:

O(f)={gg(n)Mf(n) for some M>0 for large n}.

With this definition, the world makes sense again: If f(n)=3n and g(n)=5n, then fO(n) and gO(n), but there is no equality involved so we can’t make bogus deductions like 3=5. We can however make the correct observation that O(n)O(nlogn)O(n2)O(n3), something that would be difficult to express with the equals sign.

Misconception 2: “Informally, Big-O Means ‘Approximately Equal’”

If an algorithm takes 5n2 seconds to complete, that algorithm is O(n2) because for the constant M=7 and sufficiently large n, 5n27n2. But an algorithm that runs in constant time, say 3 seconds, is also O(n2) because for sufficiently large n, 3n2.

So informally, big-O means approximately less than or equal, not approximately equal.

If someone says “Topological Sort, like other sorting algorithms, is O(n log n)”, then that is technically correct, but severely misleading, because Toplogical Sort is also O(n) which is a subset of O(nlogn). Chances are whoever said it meant something false.

If someone says “In the worst case, any comparison based sorting algorithm must make O(nlogn) comparisons” that is not a correct statement. Translated into English it becomes:

“In the worst case, any comparison based sorting algorithm must make fewer than or equal to Mnlog(n) comparisons”

which is not true: You can easily come up with a comparison based
sorting algorithm that makes more comparisons in the worst case.

To be precise about these things we have other types of notation at our disposal. Informally:

O(): Less than or equal, disregarding constants
Ω(): Greater than or equal, disregarding constants
o(): Stricly less than, disregarding constants
Θ(): Equal to, disregarding constants

and some more. The correct statement about lower bounds is this: “In the worst case, any comparison based sorting algorithm must make Ω(nlogn) comparisons. In English that becomes:

“In the worst case, any comparison based sorting algorithm must make at least Mnlog(n) comparisons”

which is true. And a correct, non-misleading statement about Topological Sort is that it is Θ(n), because it has a lower bound of Ω(n) and an upper bound of O(n).

Post external references

  1. 1
    http://www.impulsetrain.com/big-o-static/
  2. 2
    http://en.wikipedia.org/wiki/Big_O_notation#Family_of_Bachmann.E2.80.93Landau_notations
Source