views:

43

answers:

1

An algorithm having worst-case running time of O(N^2) took 30secs to run for input size N=20. How long will the same algorithm take for input size N=400 ?

+3  A: 

O(n^2) implies proportionality to the square of n (see this guide). So

 T = K (n^2)
30 = K (20^2)
 K = 30 / 400

Hence time for 400 items

   = (30 / 400)( 400 ^ 2 )

So that 12000 seconds.

Now, that's not necessarily true unless you know that the original 20 item test was a worst case scenario, if it isn't then we have a bad estimate of K. Even if if we have a good estimate of K so we we know the worst case scenarion for 400 items, we don't know that these 400 items will take that long.

djna
Thanks, I guess it just helps establish an upper bound on the running time.
Shailesh Tainwala
Note my point about the original "20 takes 30 seconds" datum. Unless there is some reason to know that this *is* a worst case input set you have not actually established an upper bound.
djna
Ahh, didn't think about that. Thanks, I've got some clarity now, it was easy for me to miss that point.
Shailesh Tainwala