Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

binary_search.py def binarySearch (listNumbers, low, high, key): # Test array def main(): array_for_test = [-8,-2,1,3,5,7,9] print(binarySearch(array_for_test,0,len(array_for_test)-1,9)) print(binarySearch(array_for_test,0,len(array_for_test)-1,-8)) print(binarySearch(array_for_test,0,len(array_for_test)-1,4)) main() The goal of this lab is

image text in transcribed

binary_search.py

def binarySearch (listNumbers, low, high, key):

# Test array

def main():

array_for_test = [-8,-2,1,3,5,7,9]

print(binarySearch(array_for_test,0,len(array_for_test)-1,9))

print(binarySearch(array_for_test,0,len(array_for_test)-1,-8))

print(binarySearch(array_for_test,0,len(array_for_test)-1,4))

main()

The goal of this lab is to practice writing recursive functions. A recursive function is a function that calls itself with a smaller problem size. A recursive function a stopping condition, a problem case simple enough that calling the function again is not necessary uestion 1: Binary Searc 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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 2 Lnai 9285

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Joao Gama ,Alipio Jorge ,Carlos Soares

1st Edition

3319235249, 978-3319235240

More Books

Students also viewed these Databases questions

Question

How autonomous should the target be left after the merger deal?

Answered: 1 week ago