""" Binary search - efficient searching in a sorted array """ def binary_search(a, value) : """Return the index in which the given value appears in the sorted array a """ left = 0 right = len(a) - 1 while left <= right : mid = (left+right)/2 if value == a[mid]: return mid else: if value < a[mid]: right = mid-1 else: left = mid+1 return -1 print binary_search(range(100), 20) print binary_search(range(1, 100, 2), 20)