-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.clj
More file actions
30 lines (26 loc) · 899 Bytes
/
binary_search.clj
File metadata and controls
30 lines (26 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(ns clojurelogic.binary-search)
(defn binary-search [item-list item]
(if (empty? item-list)
false
(let [item-list-len (count item-list)
mid (quot item-list-len
2)]
(cond
(= item (nth item-list
mid))
true
(< item (nth item-list
mid))
(let [begin 0
new-item-list (subvec item-list
begin
mid)]
(println "new-item-list: " new-item-list)
(recur (vec new-item-list) item))
:else
(let [begin (inc mid)
new-item-list (if (> begin item-list-len)
[]
(subvec item-list
begin))]
(recur (vec new-item-list) item))))))