Reputation:
If we have 2 algorthims. One of them is O(f(x))
time-complexity and the other on is θ(f(x))
time-complexity. Which one we prefer to solve our problem? and why?
Upvotes: 1
Views: 570
Reputation: 51132
There is insufficient information given to decide which algorithm is preferable. It's possible that the first algorithm is preferable, it's possible that both are equally preferable, and it's even possible the second is preferable if they are asymptotically equal but the second has a lower constant factor.
So as you can see, it's not possible to say without more information.
Upvotes: 1
Reputation: 186813
Let's try to compare the algorithms:
First algorithm has O(nlogn)
time complexity which means that execution time t1
is
t1 <= k1 * n * log(n) + o(n * log(n))
Second algorithm is θ(nlogn)
, that's why
t2 = k2 * n * log(n) + o(n * log(n))
Assuming that n
is large enough so we can neglect o(n * log(n))
term, we still have two possibilities here.
t1 < n * log(n)
t1 = k1 * n * log(n)
(at least for some worst case)In the first case we should prefer algorithm 2
for large n
, since algorithm 1
has a shorter execution time when n
is large enough.
In the second case we have to compare unknown k1
and k2
, we have not enough information to choose from 1st and 2nd algorithms.
Upvotes: 0