Kelsey Bjarnason wrote On 08/02/07 18:57,:
Convention is to count only the most significant factor; thus an algorithm
which runs in O(N) + 1000 time is an O(N) algorithm, the constant is
discarded.
In the description above, the function should run in N + O(log N) time.
As N increases, runtime increases in proportion to it, with log N adding a
small and ever less significant portion of the runtime; thus the O(log
n) is discarded and the algorithm is thus found to be O(N).
<off-topic>
There's no such convention. Big-O expresses *any*
upper bound, at any point in an expression. (Obviously,
it's silly to include in the expression any terms of
lower order than the Big-O term.)
Do you think Knuth uses Big-O incorrectly when he
writes (for example)
H_n = ln n + gamma + O(1/n)
? This says that the nth harmonic number is roughly
equal to the natural log of n, plus a constant, and
that the approximation error is no worse than C/n for
n > N. Both C and N are unknown, but we know the error
eventually becomes and remains less than a bound that
diminishes as the inverse of n.
He *could* instead have written
H_n = ln n + O(1)
.... which says the nth harmonic number is roughly equal
to the natural log of n, with an approximation error
no worse (asymptotically) than a constant. This is true,
but less informative than the original.
If he had followed your suggestion, he would have
told us even less:
H_n = O(ln n)
.... which says the nth harmonic number is approximately
proportional to the log of n. It might be a million
times the log of n, or it might be the log of n plus
a million; all we're told is that it's approximately
proportional, with an unknown proportionality constant
and unknown contributions from lower-order terms.
He might even have written any of
H_n = O(n)
H_n = O(n^2)
H_n = O(e^n)
.... because Big-O expresses only an upper bound. A
value that is bounded by a logarithmic function is
obviously also bounded by linear, quadratic, and
exponential functions. But all these "improvements"
communicate less information than the original.
Back to programming: Can you explain why Quicksort
is usually said to be faster than Heapsort, even though
both have asymptotic average running times of O(n log n)?
If you cannot, what reason do you have to prefer one
over the other?[*]
[*] Assuming for simplicity's sake that your choice
is based solely on expected running time; there are, of
course, many other things an engineer must consider in
an actual situation.
</off-topic>