How To Calculate The Time Complexity Of The Algorithm? About Best Case, Worst Case And Average Case.

7

7 Answers

Oddman Profile
Oddman answered
You have to know
- the algorithm
- the shortest path through the algorithm
- the longest path through the algorithm
- how the path lengths or numbers of iterations vary with the number of data items processed
- what an "average" data set looks like.

The time complexity of the best case can be calculated by considering what happens to the execution time or number of iterations when you increase the dataset by 1 item, by 2 items, by a factor of 2, a factor of 3. If the execution time increase is linear with the number of data items, we say the time complexity is O(N), that is, on the order of N, the number of data items. If the execution time varies as the square of the number of data items, then we say the time complexity is O(N2). The time complexity can be of other orders, exponential, or have some other relationship to the number of data items, for example, O(N3/2).

The same assessment of time complexity can be done for the worst and average cases.
thanked the writer.
Anonymous
Anonymous commented
Can you tell me the time complexity of this satatement
while(n>0)
{
if(n%2==0)
{break;}
}
Oddman
Oddman commented
No, because there is nothing in the statements given that gives a clue how execution time changes with n or that changes the value of n. For n > 2 it will run forever.
Anonymous Profile
Anonymous answered
AS PER 1ST algorithm,
inside teh first while loop----->

we need to find out , what all basic operation which takes constant amount of time.
Here
k ← 0   ------ is one basic operation
k < n*n  ---- assume , n*n = N ( any way it will be one number) one basic opertion
  y ← y + j - k   ------ anotherbasic operation
k ← k + 1 ------ is one more basic operation
j ← j - 1 ------ last basic operation inside 1st while loop

Now consider the time,

1+ N + N -1 + N-1 + 1 = 3N

inside while loop , takes 3N ( N is just a big number if we assume a big value of n)
Y ← 0
j ← n
while j >= 1 do {
  k ← 0
  while k < n*n do {
  y ← y + j - k
  k ← k + 1
  }
j ← j - 1
}

Y ← 0
j ← n

now take 1st algorithm as a whole,

now for n value 1 , the total alogorithm will take 3N times,
now for n value 2, the total algorithm will take 2*3N times
so on...
For value   n
n * 3N =  3 N2 (three N square) so finally we can say, the oerder of this algorithm is n square.
Anonymous Profile
Anonymous answered
For(I=1;I
Anonymous Profile
Anonymous answered
Happy new year and peace to you all! Can you please explain to me what is the execution time T(n) of the 3 algorithms?

Algorithm 1:
Y ← 0
j ← n
while j >= 1 do {
  k ← 0
  while k < n*n do {
  y ← y + j - k
  k ← k + 1
  }
j ← j - 1
}
return y

Algorithm 2:
For I ← 1 to n do
  for j ← 1 to I do
  for k ← j to I+j do
  a ← a + 1
  end for
  end for
end for

Algorithm 3:
For I ← 1 to m do
  for j ← 1 to i2 do
  for k ← 1 to j do
  b ← b + 1
  end for
  end for
end for
Anonymous Profile
Anonymous answered
@Oddman, you mean if only n is not divisible by 2 .. The loop will go for ever .. =)
thanked the writer.
Anonymous
Anonymous commented
What's the initial value of n..according ..if n pass then ..the loop execute ......otherwise if n value false ..it terminate and in that case ....complexite is ..o(1)
but if n value suppose is 1 so if statement is false and ....the complexity is ..infinite .....becoz it's going to infinite loop ...

But if the n value is 2 then again complexity is o(1)
if n value is 3 then complexity is o(2)

so it's countinue ..basically complexity depend on here n.................
Anonymous Profile
Anonymous answered
For(I=0;I

Answer Question

Anonymous