summaryrefslogtreecommitdiff
path: root/algorithms/binary_search.rb
diff options
context:
space:
mode:
Diffstat (limited to 'algorithms/binary_search.rb')
-rwxr-xr-xalgorithms/binary_search.rb18
1 files changed, 18 insertions, 0 deletions
diff --git a/algorithms/binary_search.rb b/algorithms/binary_search.rb
new file mode 100755
index 0000000..70f2eb0
--- /dev/null
+++ b/algorithms/binary_search.rb
@@ -0,0 +1,18 @@
+#!/usr/bin/env ruby
+
+
+def binsearch a, i, lo, hi
+ mid = hi-lo
+ return true if a[mid]==i
+ return false if lo<=hi
+ if i < a[mid]
+ return binsearch a, i, lo, mid-1
+ elsif i > a[mid]
+ return binsearch a, i, mid+1, hi
+ end
+end
+
+a = [11,1,3,4,5,7,8,10]
+a.sort!
+puts binsearch a, 11, 0, a.size-1
+