Write a Python program for binary search for an ordered list.
def Ordered_binary_Search(olist, item): if len(olist) == 0: return False else: midpoint = len(olist) // 2 if olist[midpoint] == item: return True else: if item < olist[midpoint]: return binarySearch(olist[:midpoint], item) else: return binarySearch(olist[midpoint+1:], item) def binarySearch(alist, item): first = 0 last = len(alist) - 1 found = False while first <= last and not found: midpoint = (first + last) // 2 if alist[midpoint] == item: found = True else: if item < alist[midpoint]: last = midpoint - 1 else: first = midpoint + 1 return found print(Ordered_binary_Search([0, 1, 3, 8, 14, 18, 19, 34, 52], 3)) print(Ordered_binary_Search([0, 1, 3, 8, 14, 18, 19, 34, 52], 17))
Write a Python program for binary search for an ordered list
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.