Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (in-ns 'user)
  2.  
  3. (defn sort-parts
  4.   "Lazy, tail-recursive, incremental quicksort. Works against
  5.  and creates partitions based on the pivot, defined as 'work'."
  6.   [work]
  7.   (lazy-seq
  8.    (loop [[part & parts] work]
  9.      (if-let [[pivot & xs] (seq part)]
  10.        (let [smaller? #(< % pivot)]
  11.          (recur (list*
  12.                  (filter smaller? xs)
  13.                  pivot
  14.                  (remove smaller? xs)
  15.                  parts)))
  16.        (when-let [[x & parts] parts]
  17.          (cons x (sort-parts parts)))))))
  18.  
  19. (defn qsort [xs]
  20.   (sort-parts (list xs)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement