Question: Could you use sortto write this oneThank you Question 2: Binary search (2 points) Write a python function to do a binary search in a
Question 2: Binary search (2 points) Write a python function to do a binary search in a list and return True if the target element is in the list, else return False. Binary search is a search algorithm that finds if a target value is in a sorted list. Binary search compares the target value to the middle element of array. li they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, agaln taking the mi element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, t target is not in the array The input list may not be sorted. Assume the input list is non-empty Your code must be an implementation of the binary search algorithmIn particular, you can't ust return esult by using built-in functi s such as find, index, count and related functions. You are also not allowed to import libraries. Doing so will result in zero mark to this question. In [11: def binarysearch(L, element) # Your code here In 121: binarysearch(t1, 3, 23, 6, 7, 2, 91,9)--True Traceback (most recent call last) Typetrror
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
