Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Could you use sortto write this oneThank you image text in transcribed
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 ( 1 binarysearch(, 3, 23, 6,7, 2, 91,9) True cipython-input-1-555e32a58e17> in binarysearch(L, lement) while lowercupper m. ( lower + upper ) // 2 if L(m)-element: return True elif L(m)element : TypeErrors 'list' object is not callable In [25]1 binarysearch([1, 2, 3],4)--False

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

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions