Skip to content

Instantly share code, notes, and snippets.

@ndonolli
Created August 25, 2020 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndonolli/3281e5ca0233c19eac66ce88b42d04a4 to your computer and use it in GitHub Desktop.
Save ndonolli/3281e5ca0233c19eac66ce88b42d04a4 to your computer and use it in GitHub Desktop.
(defn binary-search [target coll]
(loop [tree coll]
(let [idx (int (/ (count tree) 2))
node (nth tree idx)]
(cond
(= target node) true
(= 1 (count tree)) false
(< target node) (recur (subvec tree 0 idx))
(> target node) (recur (subvec tree idx (count tree)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment