开发者

time complexity of an algorithm

开发者 https://www.devze.com 2023-02-08 03:17 出处:网络
An algorith with size n=100 takes 21 seconds to run. With size n=1000 it takes 31 seconds and with n=10000 takes 41 seconds to run. What is the running complexity?

An algorith with size n=100 takes 21 seconds to run. With size n=1000 it takes 31 seconds and with n=10000 takes 41 seconds to run. What is the running complexity?

If I try O(n) Then: T(n)=(21*1000)/100 = 210 s (Not O(n))

If I try O(n^2) Then: T(n)=(21*1000^2)/100^2 = 2100 s (Not O(n^2))

If I try O(log n) then: T(n)=(21*log1000开发者_如何学Python)/log100=31.5 (Not O(log n))

The other option I am given is O(1/n). How do I calculate this?


looks like an O(lgn).

The time for n is T(n) = 10*log(n) + 1 when the base of the log is 10.


To solve this problem start by plotting some functions from the various classes. For example to learn about the O(n) linear class plot the function T(n)=n and to learn about the O(n^2) class plot the function T(n)=n^2. This will help you recognize the shape of the various functions.

After that, plot the points given in your questions with the values of n in the x-axis and the timed values on the y-axis. You should be able to quickly recognize the shape in this question.

Hint: It's not O(log n) :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消