vamsi
vamsi

Reputation: 29

how to estimate best, worst and average cases for time complexity?

how can we tell whether the obtaining time complexity for an algorithm is in best case or worst case or an average case? example if we get time complexity as t(n) = 2n^2 + 3n -1, how to estimate the best or worst or average cases?

Upvotes: 2

Views: 5283

Answers (4)

Zeeshan
Zeeshan

Reputation: 1

enter image description here >>Bast Case:

If you are finding the Best case Complexity then you check image and following statement.

T(n)= c1n+c2(n-1)+c4(n-1)+c5(n-1)+c8(n-1)
      (c1+c2+c5+c8)n-(c2+c4+c5+c6)

The Maximum Power of n is 1, so we are say tha Best Case Complexity is Insertion Sort O(n).

>>Worst Case:

If you are finding the Worst case Complexity then you check image and following statement.

T(n)= c1n+c2(n-1)+c4(n-1)+c5(n(n+1)/2)+c6(n(n-1)/2)+c7(n(n-1)/2)+c8(n-1)
      (c5/2 + C6/2 +C7/2)n^2 +(c1+c2+c4+ c5/2 -C6/2 -C7/2+ c8)

The Maximum Power of n is 2, so we are say tha Best Case Complexity is Insertion Sort O(n^2).

Upvotes: 0

Ricky Bobby
Ricky Bobby

Reputation: 7608

first note : t(n) = 2n^2 + 3n -1 will always be a big O(n^2) in worst, best and average case.

In some cases the complexity depends on the input of your algorithm, in these cases usually people calculate the complexity in worst case.

But when you think worst case is not relevant and too restrictive, you do an average case analysis or an amortized analysis. For example if an algorithm works in O(n) for (1-1/n)% of his inputs and O(n^2) for (1/n)%, you don't want to say it's O(n^2), and give the average complexity that will be more like O(n). But the worst case can still happen.

Look at this post for more detail on average case analysis and amortized analysis.

Difference between average case and amortized analysis

and the wikipedia's articles :

Upvotes: 2

C0L.PAN1C
C0L.PAN1C

Reputation: 12243

That simplifies to O(2n^2) or just merely O(n^2). You remove all the other elements because it simplifies.

This is known as Big O notation. This is simply the worst case time. THe others all are irrelevant for the most part.

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96258

You can only tell that by carefully examining the algorithm.

If you know that exactly t(n)=2n^2+3n-1, then t(n)=O(n^2) and that's the best, the worst and consequently the average time complexity.

Upvotes: 1

Related Questions