# Function to find the name and return the associated phone number def binarySea
ID: 3803339 • Letter: #
Question
# Function to find the name and return the associated phone number def binarySearch(names, phones, name): # left side of list left = 0 # right side of the list right = len (names) - 1 found = 0 while right >= left and found == 0: # find the middle of the list m = (left + right)//2 # Fill in the code that examines the middle # number and decides what to do next # Recall that you are going to compare m to # the number you are searching for and decide how # to search the part of the list where the name # should be. #of the list to search next if found == 0: return "" else return phones[m]Explanation / Answer
int binarySearch( names, phones, name)
{
left=0;
right=
while (left <= right && found=0)
{
int m = left + (right-left)/2;
// Check if name is present at mid
if (names[m] == name)
found++;
// If name greater, ignore left half
if (names[m] < name)
left = m + 1;
// If name is smaller, ignore right half
else
right = m - 1;
}
if(found==0)
return 0;
else
return phone[m];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.