Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 5 . 1 4 LAB: Binary Search Binary search can be implemented as a recursive algorithm. Each call makes a recursive call on one
LAB: Binary Search
Binary search can be implemented as a recursive algorithm. Each call makes a recursive call on onehalf of the list the call received as an argument.
Complete the recursive function binarysearch with the following specifications:
Parameters:
a list of integers
a target integer
lower and upper bounds within which the recursive call will search
Return value:
if found, the index within the list where the target is located
if target is not found
The algorithm begins by choosing an index midway between the lower and upper bounds.
If target numsindex return index
If lower upper, return to indicate not found
Otherwise call the function recursively with half the list as an argument:
If numsindex target, search the list from index to upper
If numsindex target, search the list from lower to index
The list must be ordered, but duplicates are allowed.
Once the search algorithm works correctly, add the following to binarysearch:
Count the number of calls to binarysearch
Count the number of times when the target is compared to an element of the list. Note: lower upper should not be counted.
Hint: Use a global variable to count calls and comparisons.
The input of the program consists of integers on one line followed by a target integer on the second.
The template provides the main program and a helper function that reads a list from input.
Ex: If the input is:
the output is:
index: recursions: comparisons:
qxzqy
LAB ACTIVITY
: LAB: Binary Search
main.py
Load default template...
Develop mode
Submit mode
When done developing your program, press the Submit for grading button below. This will submit your program for autograding.
Submit for grading
Coding trail of your work
What is this?
M
R
M
T
min:
Latest submission : AM KST on
Total score:
Download this submission
:Compare output
Input
Your output
index: recursions: comparisons:
:Compare output
Input
Your output
index: recursions: comparisons:
:Compare output
Output differs. See highlights below.
Input
Your output
index: recursions: comparisons:
Expected output
index: recursions: comparisons:
:Compare output
Input
Your output
index: recursions: comparisons:
:Unit test
Test binarysearch with and target Should return
Test feedback
binarysearch correctly returned
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started