summaryrefslogtreecommitdiff
path: root/algorithms/binary_search1.py
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-06-14 16:46:27 +0200
committerPatrick Simianer <p@simianer.de>2014-06-14 16:46:27 +0200
commit26c490f404731d053a6205719b6246502c07b449 (patch)
tree3aa721098f1251dfbf2249ecd2736434c13b1d48 /algorithms/binary_search1.py
init
Diffstat (limited to 'algorithms/binary_search1.py')
-rwxr-xr-xalgorithms/binary_search1.py16
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)
+