-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,185
  • 3
  • 17
  • 39

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,023
  • 1
  • 19
  • 29
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,092
  • 2
  • 22
  • 37