3

For the given code (I am using just one from my previous questions), the running time using O notation is O(n^2). If I want to express the running time using Theta notation would it be the same? Meaning Theta(n^2)?

for(int i=0; i<N; i++){
   for(int j=1; j<N; j++){

    System.out.println("Yayyyy");
    if(i<=j){
        System.out.println("Yayyy not");
    }
}
}
FranXh
  • 4,481
  • 20
  • 59
  • 78

2 Answers2

0

In essence: Big O-notation is for UPPER bounds for running time. This means that most algorithms have several Big O-bounds (your algorithm is for example O(n^23) because it is by far more effective than a theta(n^23) algorithm)
Theta-notation is for tight bounds. Not all algorithms have a clearly defined tight bound, because this would mean that it grows proportionally with the other function. In your example, because there is no way the algorithm can finish without having printed "Yayyy not" (n^2 - n)/2 times, and it will never run more than this number of times, it will always grow proportionally with n^2, and thus have a theta(n^2) bound!

olagjo
  • 2,125
  • 2
  • 14
  • 16
0

To make this short and palatable, BigO(n^2) means that your algorithm will not take longer than ~n^2 time. BigTheta(n^2) means that your algorithm will not take longer than ~n^2 time, and it will not take less than ~n^2 time.

Eli Burmin
  • 26
  • 5