Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions

Question

Types of cultural maps ?

Answered: 1 week ago

Question

Discuss the various types of leasing.

Answered: 1 week ago

Question

Define the term "Leasing"

Answered: 1 week ago

Question

What do you mean by Dividend ?

Answered: 1 week ago