I've just started learning some basic algorithms, but there’s something about the binary search algorithm that’s been confusing me.
From what I understand the max time complexity of a binary search is O(log(n)) where log is base 2.
However, when using the formula on values of N that is not a power of 2, you get a non-integer value.
What I am confused about is if you end up with, let’s say 3.3, is that a maximum of 3 steps or 4 steps.
You get the value 3.3 when using an array where n = 10. That being said, I manually counted the number of steps and I got 4, so I’m assuming you round up.
But in my textbook, it says an array where n=10000, takes a max of 13 steps. When putting that in the log formula above we get 13.2, which means in this case we rounded down.
I’ve tried a binary search with arrays of different sizes and I get instances where I must round down to get the textbook answer and instances when I must round up.
What I am unsure of is when to round up or down, or if I making another mistake entirely.
If anyone is to give me an example, could you please use an array of size 100000. Since in the book it says a maximum of 16 times, but when I manually half 100000 until I get 1 I get 17 times.
Thanks in advance!