Skip to content

Instantly share code, notes, and snippets.

@tantaman
Created October 13, 2011 21:49
Show Gist options
  • Save tantaman/1285639 to your computer and use it in GitHub Desktop.
Save tantaman/1285639 to your computer and use it in GitHub Desktop.
exercies: binary search - clojure
(defn bsrch [needle haystack]
(loop [low (int 0) high (count haystack) mid (quot (count haystack) 2)]
(if (= (haystack mid) needle)
mid
(if (= high low)
-1
(if (> needle (haystack mid))
(recur (int (inc mid)) high (quot (+ mid high) 2))
(recur low (int (dec mid)) (quot (+ low mid) 2))
)))
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment