Question: 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

 In Python please inar You have seen the binary search in

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!