Sha256: f9fe7321b89c35b383b76fa68585b6debd0f54b46844c843e227bcd1580a4bf2

Contents?: true

Size: 460 Bytes

Versions: 396

Compression:

Stored size: 460 Bytes

Contents

(ns binary-search)

(defn middle [alist]
  (-> alist (count) (quot 2)))

(defn search-for
  [elem alist]
  (let [middle (middle alist)
        cur-elem (nth alist middle)]
    (cond
      (= cur-elem elem) middle
      (or (= middle (count alist)) (zero? middle)) (throw (Exception. (format "%s not found in list" elem)))
      (< cur-elem elem) (+ middle (search-for elem (drop middle alist)))
      (> cur-elem elem) (search-for elem (take middle alist)))))

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.138 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.137 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.136 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.135 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.134 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.133 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.132 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.131 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.130 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.129 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.128 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.127 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.126 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.125 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.124 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.123 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.122 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.121 tracks/clojure/exercises/binary-search/src/example.clj
trackler-2.2.1.120 tracks/clojure/exercises/binary-search/src/example.clj