Search code examples
algorithmperformancetime-complexitybig-opseudocode

What is the time complexity of this function? is it n*log(n)?


I'm finding difficulty to find the time complexity of this pseudocode function

f(n):
    count = 0
    
    for (i = 0; i < n; ++i)
        for (j = i; j > 0; --j)
            count++
end;

It's not O(n) but it grows faster than O(n) and it is not O(n^2) but it grows slower than O(n^2), is it n*log(n)?


Solution

  • The outer loop has O(n). The inner loop is also O(n), although it executes on average only half as much operations as the outer loop. But constant factors are ignored for the complexity. The complexity is thus O(n^2).