Created
October 13, 2011 21:49
-
-
Save tantaman/1285639 to your computer and use it in GitHub Desktop.
exercies: binary search - clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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