# BinarySearch.py is a simple binary search program for python. It accepts an # ordered list of ints and an int to look for in that list and returns the index # of the number if found, if not found it returns -1 # Preconditions:vals must be of an array of sorted ints (in increasing order) # and i must be an int. # Postconditions: BinarySearch returns the index of the i or -1 if not found import string #Binary Search def binary_search(i, vals): #keep track of the end and beginning end = len(vals) beg = 0 #keep looking at smaller portions of the list until i is found while end > beg: mid = (beg+end)//2 midvalue = vals[mid] if midvalue < i: beg = mid +1 elif midvalue > i: end = mid elif midvalue == i: return mid return -1