-1

How does one go about finding the best case time complexities for formulae like 2n², 3⋅log₂(n) and 2n² + 10n? What is the exact procedure?

AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
jimo
  • 430
  • 2
  • 7
  • 19

1 Answers1

0

For a fixed formula, best, average and worst case are identical. The complexities would be

  • 2n² ∈ Θ(n²)
  • 3⋅log₂(n) ∈ Θ(log n)
  • 2n² + 10n ∈ Θ(n²)

Algorithms may have a best case complexity, which depends on the input. E.g. Take a look at bubble sort.

Input: A[0..n-1] 

switched = false;
for(i = 0; i < n; i++)
{
   switched = false;
   for(j = 0; j < n-i-1; j++)
   {
      if(A[j] > A[j+1])
      {
         switch(A,j,j+1);
         switched = true;
      }
   }

   if(!switched)
      break;
}

The worst case is a big to small sorted list which leads to a complexity of O(n²). But if your input is a small to big sorted list, the algorithm executes the inner for loop only one time and since it don't have to switch elements, the algorithm terminates. This gives you a best case complexity of O(n).

AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66