Skip to content

Commit fc620fe

Browse files
improved code style
1 parent 62d7d00 commit fc620fe

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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")
@@ -11,17 +11,32 @@
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)))

0 commit comments

Comments
 (0)