-3

what's the time complexity of the following program?

sum=0;
for(i=1;i<=5;i++)
 sum=sum+i;

and how to define this complexity in log ? i shall highly appreciate if someone explain complexity step by step. furthermore how to show in O(big o) and logn.

[Edited]

sum=0; //1 time
i=1;   //1 time
i<=5;  //6 times
i++    //5 times
sum=sum+i;//5 times

is time complexity 18? Correct?

user1911703
  • 740
  • 5
  • 8
  • 24
  • 1
    Do you have any guesses? What’s the reasoning behind them? – Ry- Oct 20 '13 at 17:57
  • You really need to read some theory and look at some examples to understand how to compute the complexity of an algorithm. Here are some pointers to some good literature and stuff: 1. http://stackoverflow.com/a/11611770/1376448 2. [CLRS order of growth](https://www.google.com/search?q=CLRS+order+of+growth). 3. Tim Roughgarden explains it fantastically on [Cousera Algorithm-1](https://www.coursera.org/course/algo). Good luck. – kiddorails Oct 20 '13 at 18:01
  • 1
    What in the world is "O(big o)"... – user541686 Oct 20 '13 at 18:14
  • @Mehrdad I was wondering the same thing. – Phillip Cloud Oct 20 '13 at 18:26

2 Answers2

2

Preliminaries

Time complexity isn't usually expressed in terms of a specific integer, so the statement "The time complexity of operation X is 18" isn't clear without a unit, e.g., 18 "doodads".

One usually expresses time complexity as a function of the size of the input to some function/operation.

You often want to ignore the specific amount of time a particular operation takes, due to differences in hardware or even differences in constant factors between different languages. For example, summation is still O(n) (in general) in C and in Python (you still have to perform n additions), but differences in constant factors between the two languages will result in C being faster in terms of absolute time the operation takes to halt.

One also usually assumes that "Big-Oh"--e.g, O(f(n))--is the "worst-case" running time of an algorithm. There are other symbols used to study more strict upper and lower bounds.

Your question

Instead of summing from 1 to 5, let's look at summing from 1 to n.

The complexity of this is O(n) where n is the number of elements you're summing together.

Each addition (with +) takes constant time, which you're doing n times in this case.

However, this particular operation that you've shown can be accomplished in O(1) (constant time), because the sum of the numbers from 1 to n can be expressed as a single arithmetic operation. I'll leave the details of that up to you to figure out.

As far as expressing this in terms of logarithms: not exactly sure why you'd want to, but here goes:

Because exp(log(n)) is n, you could express it as O(exp(log(n))). Why would you want to do this? O(n) is perfectly understandable without needing to invoke log or exp.

Phillip Cloud
  • 24,919
  • 11
  • 68
  • 88
0

First of all the loop runs 5 times for 5 inputs hence it has a time complexity of O(n). I am assuming here that values in i are the inputs for sum. Secondly you cant just define time complexity in log terms it should always in BIG O notation. For example if you perform a binary search then the worst case time complexity of that algorithm is O(log n) because you are getting result in say 3 iterations when the input arrays is 8.

Complexity = log2(base)8 = 3

now here your comlexity is in log.

AMY
  • 248
  • 1
  • 10