top of page

PROGRAMMING CHALLENGES

Recursive Binary Search to Iterative

Difficulty:

2 Medium

2.5 Recursion

Topic:

Given the pseudocode for a binary search using recursion, rewrite it to perform the same task iteratively.

A binary search works by repeatedly dividing the search range. For the iterative version, you'll need a loop that continues narrowing the range until the target is found or not.

FUNCTION binarySearch(array, low, high, target) IF high >= low MID = (low + high) / 2 IF array[MID] == target RETURN MID ELSE IF array[MID] > target RETURN binarySearch(array, low, MID-1, target) ELSE RETURN binarySearch(array, MID+1, high, target) ENDFUNCTION

Need help with your programming skills?

If you need more help than just independent practise, then we're here for you. Book a 1:1 with us and we will be able to guide you to becoming a proficient programmer who can tackle any of the challenges an exam board can throw at you.

bottom of page