Learn
Anything recursive can be written iteratively.
As a final exercise, we’ll implement the binary search algorithm using iteration.
Our strategy remains largely the same as the recursive approach which used pointers.
Instead of recursive calls, we’ll substitute a while
loop.
Instructions
1.
Complete the binary_search()
skeleton.
You’ll need to:
- Fill in the condition for the
while
loop - Calculate the middle index using pointers
- Set the
left_pointer
when appropriate - Set the
right_pointer
when appropriate
Sign up to start coding
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.