Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Python please inar You have seen the binary search in class. However, the algorithm you saw is iterative using high and low indices that

image text in transcribed

In Python please

inar You have seen the binary search in class. However, the algorithm you saw is iterative using high and low indices that move towards each other closing the gap on where the key may exist in the container. The idea of the binary search can be described as following: (1) Compare x with the middle element. (2) If x matches with middle element, we return the middle index. (3) If x is greater than the middle element, then x can only lie in the right half subarray, and we do search in that part. 4) Similarly, if x is less than the middle element, we do search in the left subarray. Whenever we search in the smaller subarray, the same principle applies. You will need to implement a recursive binarySearch) function which takes 4 arguments: listNumbers, which is an array assumed to contain elements in ascending order; low and high, which specify the range we want to search in the array; key, which is the element we are searching for. The function should return the index of the searched element if the element is found, or return -1 if the element is not in the array. Test you function as follows: # Test array def main(): array_for_test [-8,-2,1,3,5,7,9] print (binarySearch(array_for_test,e,len(array_for_test)-1,9)) print(binarySearch(array_for test,e,len(array_for_test)-1,-8)) print(binarySearch(array_for test,e,len(array_for_test)-1,4)) main() The output for these three cases should be 6, 0,-1. Use the debugger to trace the recursion calls to figure out what is happening

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

Step: 3

blur-text-image

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

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

More Books

Students also viewed these Databases questions

Question

Methods of Delivery Guidelines for

Answered: 1 week ago