I am just a bit confused. If time complexity of an algorithm is given by
what is that in big O notation? Just or we keep the log?
So, O(N*log(N)) is far better than O(N^2) . It is much closer to O(N) than to O(N^2) . But your O(N^2) algorithm is faster for N < 100 in real life. There are a lot of reasons why it can be faster.
N log2N - time involves applying a logarithmic algorithm N times. Quadratic time - algorithms of this type typically involve applying a linear algorithm N times.
It has a guaranteed running time complexity of O ( n l o g ( n ) ) O(n \ log (n)) O(n log(n)) in the best, average, and worst case. It essentially follows two steps: Divide the unsorted list into sub-lists until there are N sub-lists with one element in each ( N is the number of elements in the unsorted list).
A function with a quadratic time complexity has a growth rate of n2. If the input is size 2, it will do four operations. If the input is size 8, it will take 64, and so on.
If that's the time-complexity of the algorithm, then it is in big-O notation already, so, yes, keep the log. Asymptotically, there is a difference between O(n^2)
and O((n^2)*log(n))
.
A formal mathematical proof would be nice here.
Let's define following variables and functions:N
- input length of the algorithm,f(N) = N^2*ln(N)
- a function that computes algorithm's execution time.
Let's determine whether growth of this function is asymptotically bounded by O(N^2)
.
According to the definition of the asymptotic notation [1], g(x)
is an asymptotic bound for f(x)
if and only if: for all sufficiently large values of x
, the absolute value of f(x)
is at most a positive constant multiple of g(x)
. That is, f(x) = O(g(x))
if and only if there exists a positive real number M
and a real number x0
such that
|f(x)| <= M*g(x) for all x >= x0 (1)
In our case, there must exists a positive real number M
and a real number N0
such that:
|N^2*ln(N)| <= M*N^2 for all N >= N0 (2)
Obviously, such M
and x0
do not exist, because for any arbitrary large M
there is N0
, such that
ln(N) > M for all N >= N0 (3)
Thus, we have proved that N^2*ln(N)
is not asymptotically bounded by O(N^2)
.
References:
1: - https://en.wikipedia.org/wiki/Big_O_notation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With