What does O(log n) mean exactly?

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

    Actually, if you have a list of n elements, and create a binary tree from that list (like in the divide and conquer algorithm), you will keep dividing by 2 until you reach lists of size 1 (the leaves).

    At the first step, you divide by 2. You then have 2 lists (2^1), you divide each by 2, so you have 4 lists (2^2), you divide again, you have 8 lists (2^3)and so on until your list size is 1

    That gives you the equation :

    n/(2^steps)=1 <=> n=2^steps <=> lg(n)=steps

    (you take the lg of each side, lg being the log base 2)

提交回复
热议问题