Here is my thought:
You have N
elements to begin with.
But you aren't going through each one though?
Say I have
{1, 2, 3, 4, 5, 6, 7, 8, 9}
and I want to find 4
.
The first step is looking at 5
, we see 4 < arr[5]
. Then, we have
{1, 2, 3, 4, 5}
, the middle is 3
, we see 4 > arr[2]
, thus we are left with {3, 4, 5}
.
Now we will get 4
.
But that was only 3 steps! I am not understanding why the first search takes N
elements, when we are looking at the (N-1)/2
th element, which is one step?
EDIT!!!
Here is what I am taught:
search 1: n elements in search space
search 2: n/2 elements in search space
search 3: n/4 elements in search space
... search i: 1 element in search space.
search i has n/(2^[i-1])elements, thus you solve for i then you get
i = log(n) + 1.
What I don't understand:
You have n elements, I agree, but you aren't searching through all of them, you are only searching 1 element, then why do you count all n?