summaryrefslogtreecommitdiff
path: root/algorithms/binary_search1.py
blob: ea864aeeb19117b26d4624ceb1c9ca70f443ff94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python2


def binsearch(a, v, left, right):
    mid = (right-left)/2 + left
    if mid > right: return 0
    if a[mid] == v:
        return 1
    elif v > a[mid]:
        return binsearch(a, v, mid+1, right)
    elif v < a[mid]:
        return binsearch(a, v, left, mid-1)

a = [1, 2, 3, 4, 5 , 7, 8, 9, 10, 11]
print binsearch(a, 10, 0, len(a)-1)