Main.BinarySearch History
Hide minor edits - Show changes to markup
February 25, 2010, at 06:35 PM MST
by -
Added lines 1-27:
(:source lang=python:)
""" 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)
(:sourceend:)
