Question

In: Computer Science

Task 2: Recursive Binary Search Part A - Target in list? Write a function simple recursive...

Task 2: Recursive Binary Search Part A - Target in list? Write a function simple recursive binary search(lst, target) which returns True if target is in lst; otherwise False. You must use recursion to solve this problem (i.e. do not use any loops).

Part B - Where is target? Write a function advanced recursive binary search(lst, target, lo, hi) which returns the index of an instance of target in lst between lo and hi, or None if target is not in that range. When first calling the function, lo = 0 and hi = len(lst)-1. You must use recursion to solve this problem (i.e. do not use any loops).

PYTHON CODE

Solutions

Expert Solution

Check the Image for Indentation, In both the problem . RECURSION WAS USED

def BinarySearchRecursive(lst, target):

    if len(lst) == 0:

        return False

    else:

        middle = len(lst)//2

        if (target == lst[middle]):

            return True

        else:

            if target > lst[middle]:

                return BinarySearchRecursive(lst[middle+1:],target)

            else:

                return BinarySearchRecursive(lst[:middle],target)

------------------------------------------------------------------------------------------------------------------------------------

def BinarySearch(lst,target, lo, hi):

    if hi < lo: return -1     

    mid = (lo + hi)   

    if target == lst[mid]:

        return mid             

    elif target < lst[mid]:

        return BinarySearch(lst, target, lo, mid - 1)

    else:

        return BinarySearch(lst, target, mid + 1, hi)

my_list=[1,5,9,10,55]   

print(my_list)

print("Index at",BinarySearch(my_list,1,0,4))

print(BinarySearchRecursive(my_list,1))

------------------------------------------------------------------------------------------------

SEE CODE FOR INDENTATION

Thanks, PLEASE COMMENT if there is any concern. PLEASE UPVOTE


Related Solutions

In the recursive version of binary search each function call reduces the search size by half....
In the recursive version of binary search each function call reduces the search size by half. Thus in the worst case for a sorted list of length n. The algorithm makes log n calls. Is it possible to make fewer than log n calls? Group of answer choices 1) Yes, depends on when it finds the element it is looking for 2) No, it will always make log n calls.
Write a function that takes a binary search tree as input and produces a linked list...
Write a function that takes a binary search tree as input and produces a linked list of the entries, with the entries sorted (smallest entries at the front of the list and largest entries at the back). *Hint: use in-order traversal.* C++ there is no any structure
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of comparable objects L, and a new item x, find the location (index) of x or indicated that x is not in L. 5a) Give a recursive algorithm for binary search. No, while, for, repeat etc statements are allowed. Only if, if else and assignment statements are allowed. 5b) Write a difference equation for the running time of your Binary Search algorithm. Solve your equation...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
For C++ Function 1: Write a recursive function to perform a sequential search on a set...
For C++ Function 1: Write a recursive function to perform a sequential search on a set of integers The function will require an array parameter and the number to look for. These are the minimal parameter requirements The function should take an array of any size Function 2: Write a recursive function that will convert an integer (base 10) to binary The function should only have an integer parameter Have the function write the binary number to the console You...
Write a modification of the recursive binary search algorithm that always returns the smallest index whose...
Write a modification of the recursive binary search algorithm that always returns the smallest index whose element matches the search element. Your algorithm should still guarantee logarithmic runtime. Give a brief discussion of the best- and worst-case runtimes for this new algorithm as they compare to the original. NOTE: You do not have to re-write the entire algorithm. You just need to indicate any changes you would make and show the pseudocode for any portions that are changed. Example: Given...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[],...
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT