What does O(log n) mean exactly?

后端 未结 30 3188
执念已碎
执念已碎 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:53

    O(log n) is a bit misleading, more precisely it's O(log2 n), i.e. (logarithm with base 2).

    The height of a balanced binary tree is O(log2 n), since every node has two (note the "two" as in log2 n) child nodes. So, a tree with n nodes has a height of log2 n.

    Another example is binary search, which has a running time of O(log2 n) because at every step you divide the search space by 2.

提交回复
热议问题