Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Computer Science Introduction to Algorithms Algorithms in Code Binary Search Implementations

Calvin Secrest
Calvin Secrest
24,812 Points

Recursive binary search with iterative

So if we continue with this code snippet and tried to verify the call would it look like this:

def recursive_binary_search(list, target, start=0, end=None):
    if end is None:
        end = len(list) - 1
    if start > end:
        return -1

    mid = (start + end) // 2

    if target == list[mid]:
        return mid
        if target < list[mid]:
            return recursive_binary_search(list, target, start, mid-1)
            return recursive_binary_search(list, target, mid+1, end)

# The if statement returns the index position of the target if found, else returns None

def verify(result, index):
  print("Target found: ", result)

  elif index is not None:
      print("Target found at index: ", index)

    print("Target not found in list")

numbers = [1,2,3,4,5,6,7,8]
result = recursive_binary_search(numbers, 12)

result = result = recursive_binary_search(numbers, 6)