;; 时间复杂度从 O(n log(n)) 到 O(n^2)
;; 空间复杂度 O(log(n))
;; 无需摆弄索引:filter 按基准把尾部切开
(defn quick-sort [coll]
(if (< (count coll) 2)
(vec coll)
(let [pivot (first coll)
others (rest coll)]
(into (conj (quick-sort (filter #(< % pivot) others)) pivot)
(quick-sort (filter #(>= % pivot) others))))))
(def items [4 1 5 3 2])
(def sort-items (quick-sort items))
;; sort-items is [1 2 3 4 5]
(prn items)
(prn sort-items)
(println "same as built-in sort:" (= sort-items (sort items)))