Question: come up with a program that employs binary search to report back if a key has been found in a sequence of numbers #INPUT:
come up with a program that employs binary search to report back if a key has been found in a sequence of numbers #INPUT: take in a single integer key value, and a sequence of non negative data terminated by a -1. Assume the data will be given in sorted order. Example: 2, 8, 10, 22, 45, 100. Remember the terminating value of -1 does NOT count as data. #OUTPUT: if the key is found, output a message and the location (index) where it was found. If the key is not found, report back that your program failed to find it. #A skeleton code is given below, please note that the binary_search function is still NOT complete. #Finish defining the binary search function def binary_search(val, key, low, high): #what are the base cases? mid = int((low + high)/2) if(key == val[mid]): return mid if(key > val[mid]): binary search(val, key, mid+1, high) if(key < val[mid]): binary_search(val, key, low, mid-1) #Write the main calling routine below
Step by Step Solution
3.36 Rating (159 Votes )
There are 3 Steps involved in it
def binarysearchval key low high Base case 1 Key not found if low high ret... View full answer
Get step-by-step solutions from verified subject matter experts
