开发者

How To Calculate Complexity?

开发者 https://www.devze.com 2023-02-10 10:18 出处:网络
I am a beginner in algorithms and I don\'t know how to calculate comple开发者_如何学JAVAxity. Example:

I am a beginner in algorithms and I don't know how to calculate comple开发者_如何学JAVAxity.

Example:
int x=10,y;
y = x;

What is the complexity in example above?

Thanks


That should be O(1) if you refer to the O-Notation.


In the Big O Notation this corresponds to O(1) which basically means that the run-time of the operation is constant or at least is less than a certain constant. Ergo, the run-time does not depend on the input you have. As you may infer from what I wrote, the Big O Notation only gives an upper bound of the operation. There is also other notations which give a lower-bound and so on.

An example of a case where it does depend on the input could be:

int res = 0;
int[] arr = getSomeArray();
foreach (int i in arr)
    res = res + i;

Here the run-time depends on how big the array is, and if we give the length of the array the variable n then this will be O(n). Again, the Big O Notation does not specify exactly how long it will take to execute but, in this case, just says that we can multiply n by some constant and then it will be finished within n*some s.

A more detailed explanation is given here: What is a plain English explanation of "Big O" notation?

0

精彩评论

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