简体   繁体   中英

Finding lower and upper bound to complexity

I want to find lower and upper bound to complexity of this algorithm

1: for all i=1 to n*n do
2:   for all j=i to 2*i do
3:     output “hello world”
4:   end for
5: end for

By writing it down as summation and simplifying it to

f(n) = 0.5*n^4 + 1.5*n^2

It seems that the upper bound to complexity is O(n^4), since 0.5*n^4 is the most significant element.

For lower bound to complexity, I used the formula

f(n) = Ω(g(n)) if f(n) >= c * g(n), where c > 0

and it seems the lower bound is Ω(n^3) for 0 < c < 1

Is my reasoning correct for both cases? Is there an easier way to find Omega? Thank you for your time :)

I am pretty sure the comlexity of your code is static so upper and lower bound are equal.

edit: I know complexity notations from sorting algorithms . The number of iterations depends on how the sorting is done and of course the initial order of the list. Sorting algorithms are usually the fastest on already sorted lists. So there is the best case where everything is sorted, and the worst case (some disorder) which depends on the algorithm. Some algorithms will struggle with simply reversed lists, while others don't. That's why there is no perfect sorting algorithm. You may pick the best for your situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM