Hossain Shahriar
Announcement and reminder! Tentative date for final exam shown below, please choose a time slot! December 19 December 20 December 21 Topics to be covered in this lecture Search
Find a specific element from a collection of object (e.g., number, string) We assume that the collection is sorted Two well known types Iterative search Binary search Iterative search We compare a key with each element of a collection and A for loop is required
Search (iterative) def iterative (collection, key): for i in range(len(collection)): if collection[i] == key: return True return False def main(): collection = [1, 3, 6, 8, 10] #list of numbers key = 18 #element to be searched found = iterative(collection, key) if found == True: print key, " found" else: print key, " not found" main()
Search (binary) Binary search We divide the collection into two equal parts Search either left or right part depending on the key value A while loop is required Algorithm Initialize low and up with lower and upper boundary of the list Get the middle element If the middle element equals to the key, then stop Otherwise If key is less than middle element, update low and up with low and mid+1, respectively If key is higher than middle element, update low and up with mid+1 and up, respectively
Search (binary) List = [1, 3, 5, 7, 9, 13, 24], key = 3 Key in lower half S0: low=0, up = 7 S1: mid=(low + up)/2=(0+7)/2=4, List[mid-1]=7, key=3, low=0, up =mid+1=5 S2: mid = (0+3)/2 = 1, List [mid-1] = 1, key =3, low=mid=1, up = 3 S3: mid = (1+3)/2 = 2, List [mid-1] = 3, key =3, match!!
Search (binary) List = [1, 3, 5, 7, 9, 13, 24], key = 13 Key in upper half S0: low=0, up = 7 S1: mid=(low + up)/2=(0+7)/2=3, List[mid-1]=5, key=13, low=3, up=7 S2: mid = (3+7)/2 = 5, List [mid-1] = 9, key =13, low=mid=5, up = 7 S3: mid = (5+7)/2 = 6, List [mid-1] = 13, key =13, match!!
Search (binary) List = [1, 3, 5, 7, 9, 13, 24], key = 100 Key not in the list S0: low=0, up = 7 S1: mid=(low + up)/2=(0+7)/2=3, List[mid-1]=5, key=100, low=3, up=7 S2: mid = (3+7)/2 = 5, List [mid-1] = 9, key =100, low=mid=5, up = 7 S3: mid = (5+7)/2 = 6, List [mid-1] = 13, key =100, low=mid=6, up =7 S4: mid = (6+7)/2 = 6, List [mid-1] = 13, key =100, low=mid=6, up =7 S5: mid = (6+7)/2 = 6, List [mid-1] = 13, key =100, low=mid=6, up =7 … oops!! we will stuck in the loop forever if a key is not in list Due to our round down operation We better do round up operation mid = int (math.ceil((low+up)/2.0))
Search (binary) def binary (collection, key): low=0 up = len(collection)-1 mid=0 while low < = up: mid = int(math.ceil((low+up)/2.0)) #print mid, low, up if collection[mid-1] == key: return True else: if key < collection[mid-1]: # the element could be in lower half up = mid +1 elif key >= collection[mid-1]: # the element could be in upper half low = mid +1 return False
Search (binary) Take home assignment # 4 Write a binary search algorithm Accept a list of string and a key string Return True or False, if the key is found or not Example: L = [‘have’, ‘I’, ‘just’, ‘learnt’, ‘searching’] Key=“have’, Binary(L, key) = True Key=“hello’, Binary(L, key) = False Hints: see how to compare two string
Quiz on Wednesday (Nov 23) String operator and function Modular programming Our last lecture will be on Sorting!!