Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A binary search looks for in the numbers from to (in order). How many steps does it need? What is the largest number of steps that it could ever need for items?
Each step halves the range. The worst case is about the number of times you can halve n.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
Plan. Each step looks at the middle and halves the range.
Answer: steps for . In the worst case, steps, because is the first power of above .
Log in to join the discussion.
Loading…