File tree Expand file tree Collapse file tree 1 file changed +20
-5
lines changed
binary-search/src/binary_search Expand file tree Collapse file tree 1 file changed +20
-5
lines changed Original file line number Diff line number Diff line change 11(ns binary-search.core
22 (:gen-class ))
33
4- (defn bsearch
4+ (defn bsearch1
55 [k list]
66 (if (= (count list) 0 )
77 (println " Element" k " is not present in list" )
1111 left (first split-list)
1212 right (last split-list)]
1313 (cond (= k current) (println " Element" k " is present in list" )
14- (< k current) (bsearch k left)
15- :else (bsearch k right)))))
14+ (< k current) (bsearch1 k left)
15+ :else (bsearch1 k right)))))
16+
17+ (defn bsearch2
18+ [k list]
19+ (loop [new-list list]
20+ (let [size (count new-list)
21+ mid (/ size 2 )
22+ current (nth new-list mid)
23+ split-list (split-at mid new-list)
24+ left (first split-list)
25+ right (last split-list)]
26+ (cond (= k current) (println " Element" k " is present in list" )
27+ (< k current) (recur left)
28+ :else (recur right)))))
1629
1730(defn -main
1831 [& args]
19- (bsearch 3 '(1 2 3 4 5 6 7 8 9 10 )))
32+ (bsearch1 4 '(1 2 3 4 5 6 7 8 9 10 ))
33+ (bsearch2 4 '(1 2 3 4 5 6 7 8 9 10 )))
2034
2135
2236; ; (defn -main
2337; ; [& args]
2438; ; (let [a (map read-string args)
2539; ; f (first a)
2640; ; r (rest a)]
27- ; ; (bsearch f r)))
41+ ; ; (bsearch1 f r)
42+ ; ; (bsearch2 f r)))
You can’t perform that action at this time.
0 commit comments