Arantium Maestum

プログラミング、囲碁、読書の話題

Clojureでバブルソート

とりあえず書きなぐったもの。

(defn bubble [xs i]
  (loop [left                []
         x1                  (first xs)
         [x2 & right :as xs] (rest xs)]
    (cond (>= i (count xs)) 
          (doall (concat left [x1] xs))
          
          :else
          (let [[maxx minx] ((juxt max min) x1 x2)]
            (recur (conj left minx) maxx right)))))
          
(defn bubble-sort [xs]
  (loop [xs xs, i 0]
    (if (>= i (count xs))
      xs
      (recur (bubble xs i) (inc i)))))

ちょっと何をやっているかわかりにくい。あと異様に遅い。bubblesortだし仕方ないか?とも思ったのだが試してみたところO(n3)のようだ。

どう変えるかパッと思いつかないが、近いうちにリファクタしたい。