Skip to content

Instantly share code, notes, and snippets.

Last active June 18, 2020 10:47
What would you like to do?
Binary Search in Clojure
(defn binary-search
"Finds a value in a sorted seq in log(N) time."
[coll target]
(if (seq coll)
(let [half (int (/ (count coll) 2))
pivot (nth coll half nil)]
(if (= pivot target)
(if (< target pivot)
(binary-search (take half coll) target)
(binary-search (drop (inc half) coll) target))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment