-2

What is the difference between O(N^2) and Θ(N^2)? I know that O is the upper bound and Θ is tight bound. Could someone explain me this concept with an example.

2 Answers2

2

This notation has nothing to do with the difference between best/average/worst case complexity. You can apply both notations to each of them.

An algorithm which has linear runtime is in both O(n) and O(n^2), since O only denotes an upper bound. Any algorithm that's asymptotically faster than n^2 is also in O(n^2). Mathematically O(n^2) is a set of functions which grows at most as fast as c * n^2. For example this set contains c, c * x, c * x^1.5, c x^2, c1 * x^2 + c2 * x for any c.

Θ on the other hand is both a lower and an upper bound. So a linear runtime algorithm is in Θ(n) but not in Θ(n^2). Mathematically Θ(n^2) is a set of functions which don't grow faster that c*n^2 for some c but also doesn't grow slower than c*n^2 for another smaller c. For example this set contains c * x^2, c1 * x^2 + c2 * x, arctan(x) * x^2 for any positive c and other functions where the fastest growing term grows like c * x^2 but not c, c x, c x^1.5 because all the terms grow slower than c * x^2.

See Big O notation on wikipedia

CodesInChaos
  • 5,847
0

Compare Selection Sort and QuickSort.

Selection sort has a n^2 behavious in both the best and worst case. It will always perform bad.

Quicksort on the other hand, might perform with n^2 complexity if the odds are against you, but it might (and in most cases will) perform with n*log(n) complexity.

Note how both algorithms are O(n^2) in the worst case, however only Selection Sort is Θ(n^2). This is essentially saying the above - we can bound Selection Sort running time from below by the same function, whereas Quicksort may perform asymptotically better in the best case vs worst case.

Robert Harvey
  • 200,592
Ordous
  • 1,917
  • 13
  • 12