- #1
ducmod
- 86
- 0
Homework Statement
Hello!
Binary search requires O( log2(n) ) operations in the worst case. My math doesn't add up.
I would be grateful for help.
Homework Equations
For example:
Find number 8 in the set 1234578.
The Attempt at a Solution
The set contains n = 7 elements.
1) take the middle number 4; 4 < 8
2) take the middle number in the right half of the set 7; 7 < 8
3) in the next right half we have only 1 element and it's 8. done.
Thus I've made 3 operations to find the element 8. 2 to the power 3 equals 8, but
there are only 7 elements in the set. Hence, O(log2(n) - 1) operations.
I don't see why it is log2(n).
Thank you!