/
q.clj
36 lines (26 loc) · 977 Bytes
/
q.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
(ns joy.q)
(defn rand-ints [n] (take n (repeatedly #(rand-int n))))
(defn sort-parts
"Lazy, tail-recursive, incremental quicksort. Works against
and creates partitions based on the pivot, defined as 'work'."
[work]
(lazy-seq
(loop [[part & parts] work] ;; #: Pull apart work
(if-let [[pivot & xs] (seq part)]
(let [smaller? #(< % pivot)] ;; #: Define pivot comparison fn
(recur (list*
(filter smaller? xs) ;; #: Work all < pivot
pivot ;; #: Work pivot itself
(remove smaller? xs) ;; #: Work all > pivot
parts))) ;; #: cancat parts
(when-let [[x & parts] parts]
(cons x (sort-parts parts))))))) ;; #: Sort rest if more parts
(defn qsort [xs]
(sort-parts (list xs)))
(comment
(rand-ints 10)
(qsort [2 1 4 3])
;;=> (1 2 3 4)
(qsort (rand-ints 20))
(take 10 (qsort (rand-ints 10000)))
)