September+9th+2016+by+Matt+An

September 5th 1. the use of bisects in python binary search: when we want to search an element in a list, we can take the middle value, find out the target is bigger than the middle or not, then we can abandon half of a list. keep on doing it, we can find it at last. the maximum time we take is log2n. 2. the example 2.1 iteration binary search use iteration to make a binary search function, it's faster than recursion in binary search. but iteration use counter as the condition of a loop, when all the number count off, the loop will finish. code format="python" def search_binary_iter(xs, target): low=0 high=len(xs)-1 while low<=high: middle=(low+high)//2 if xs[middle]target: high=middle-1 else: return middle return -1 code 2.2 recursive binary search recursion use the output as input again, the code will shorter than iteration, but it's slower,use more RAM. code format="python" def search_binary_recur(xs, target): low=0 high=len(xs)-1 middle=(low+high)//2 if low==high: return -1 else: if xs[middle]==target: return middle elif xs[middle]>target: return search_binary_recur(xs[:middle,target]) elif xs[middle]<target: return search_binary_recur(xs[middle:,target]) else: return -1 code