Overly simple implementation of selection sort. This is intended to be adapted from Clojure to Sapphire Lisp.

形式
Lisp
投稿日時
2020-09-16 05:39
公開期間
無期限
  1. (defrecord find-result [x ^long n])
  2. (defn find-next [lst mx x ^long n]
  3. (if (empty? lst)
  4. (find-result. x n)
  5. (let [h (first lst) t (rest lst)]
  6. (if (nil? x)
  7. (if (< h mx)
  8. (recur t mx h 1)
  9. (recur t mx nil 0))
  10. (if (or (< h x) (>= h mx))
  11. (recur t mx x n)
  12. (recur t mx h (if (= x h) (+ n 1) 1)))))))
  13. (defn find-largest2 [lst x n]
  14. (if (empty? lst)
  15. (find-result. x n)
  16. (let [h (first lst) t (rest lst)]
  17. (if (< h x)
  18. (recur t x n)
  19. (recur t h (if (= x h) (+ n 1) 1))))))
  20. (defn find-largest [lst]
  21. (find-largest2 (rest lst) (first lst) 1))
  22. (defn dup-list [dst x ^long n]
  23. (if (= 0 n)
  24. dst
  25. (recur (cons x dst) x (- n 1))))
  26. (defn skip-eq [lst x]
  27. (if (or (empty? lst) (not (= (first lst) x)))
  28. lst
  29. (recur (rest lst) x)))
  30. (defn selection-sort2 [lst tmp prev]
  31. (let [
  32. tail (skip-eq lst prev)
  33. that (find-next tail prev nil 0)
  34. x (:x that)]
  35. (if (nil? x)
  36. tmp
  37. (recur tail (dup-list tmp x (:n that)) x))))
  38. (defn selection-sort [lst]
  39. (let [that (find-largest lst) x (:x that)]
  40. (selection-sort2 lst (dup-list [] x (:n that)) x)))
  41. ; Test
  42. (selection-sort [1 2 3 4 5 9 11 5 1 4 7 2 1 0 0 11])
ダウンロード 印刷用表示

このコピペの URL

JavaScript での埋め込み

iframe での埋め込み

元のテキスト