From 26c490f404731d053a6205719b6246502c07b449 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Sat, 14 Jun 2014 16:46:27 +0200 Subject: init --- algorithms/binary_search.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100755 algorithms/binary_search.py (limited to 'algorithms/binary_search.py') diff --git a/algorithms/binary_search.py b/algorithms/binary_search.py new file mode 100755 index 0000000..3d158dc --- /dev/null +++ b/algorithms/binary_search.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python2 + + +def bin_search(a, val, lo, hi): + print "%d %d"%(lo, hi) + if hi < lo: + return -1 + mid = (lo+hi)/2 + if a[mid] > val: + return bin_search(a, val, lo, mid-1) + elif a[mid] < val: + return bin_search(a, val, mid+1, hi) + else: + return mid + + +a = [1, 2, 3, 4, 5, 6, 7, 8] +# 0 1 2 3 4 5 6 7 + +if bin_search(a, 3, 0, len(a)-1): + print "Yay!" +else: + print ":-(" + + -- cgit v1.2.3