What does O(log n) mean exactly?

后端 未结 30 3067
执念已碎
执念已碎 2020-11-22 01:19

I am learning about Big O Notation running times and amortized times. I understand the notion of O(n) linear time, meaning that the size of the input affects the g

30条回答
  •  我在风中等你
    2020-11-22 01:50

    The explanation below is using the case of a fully balanced binary tree to help you understand how we get logarithmic time complexity.

    Binary tree is a case where a problem of size n is divided into sub-problem of size n/2 until we reach a problem of size 1:

    And that's how you get O(log n) which is the amount of work that needs to be done on the above tree to reach a solution.

    A common algorithm with O(log n) time complexity is Binary Search whose recursive relation is T(n/2) + O(1) i.e. at every subsequent level of the tree you divide problem into half and do constant amount of additional work.

提交回复
热议问题