-1

Usually the rule is that if there is a loop of 1 to n elements then the complexity is O(n), and further nested loops are n x O(n). However, when do we say if a subroutine has complexity O(log n)?

Evil Washing Machine
  • 1,293
  • 4
  • 18
  • 43

2 Answers2

1

You can take as first example binary search. A explanation of the complexity of this algorithm can be taken from a related question how to calculate binary search complexity. It shown that the calculation of this type of complexity can be obtain from the recurrence.

Community
  • 1
  • 1
Mihai8
  • 3,113
  • 1
  • 21
  • 31
1

When in each iteration we reduce the problem size be a factor of X , we can say that the problem is O(log n)

E.g - Binary Search: in each iteration we reduce the problem size by factor of 2

Mzf
  • 5,210
  • 2
  • 24
  • 37