diff options
Diffstat (limited to 'algorithms/binary_search1.py')
-rwxr-xr-x | algorithms/binary_search1.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/algorithms/binary_search1.py b/algorithms/binary_search1.py new file mode 100755 index 0000000..ea864ae --- /dev/null +++ b/algorithms/binary_search1.py @@ -0,0 +1,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) + |