Question
question: Write a recursive function named binary_search(numbers, target_item) which takes a list of SORTED numbers and an integer as parameters and performs a recursive binary
question:
Write a recursive function named binary_search(numbers, target_item) which takes a list of SORTED numbers and an integer as parameters and performs a recursive binary search on the sorted list of numbers. The function should return True if item is contained within the list, or False otherwise. If the search value is greater than the middle element, the function should call itself with all elements in the right search space, excluding the middle element. If the search value is less than the middle element, the function should call itself with all elements in the left search space, excluding the middle element.
Each time the function is called with a non-empty list, it should print the middle index being searched.
Note: you may not use loops of any kind. You must use recursion to solve this problem. You can assume that the parameter list is not empty.
For example:
Test | Result |
---|---|
test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] print(binary_search(test_list, 3)) print(binary_search(test_list, 13)) | Middle index: 4 Middle index: 2 Middle index: 0 False Middle index: 4 True |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started