;; insertsort (defun insertsort (ll) (insertsort1 ll ()) ) (defun insertsort1 (l1 l2) (cond ((null l1) l2) (t (insertsort1 (rest l1) (insert (first l1) l2))) ) ) (defun insert (el ll) (cond ((null ll) (list el)) ((< (first ll) el) (cons (first ll) (insert el (rest ll)))) (t (cons el ll)) ) ) ;; selectsort (defun selectsort (ll) (cond ((null ll) nil) (t (selectsort1 (bubble-min-elem ll))) ) ) (defun selectsort1 (ll) (cons (first ll) (selectsort (rest ll))) ) (defun bubble-min-elem (ll) (bubble-min-elem1 ll () (min-elem ll)) ) (defun bubble-min-elem1 (l1 l2 minelem) (cond ((null l1) (cons minelem l2)) ((equal minelem (first l1)) (append (cons minelem l2) (rest l1))) (t (bubble-min-elem1 (rest l1) (append l2 (list (first l1))) minelem)) ) ) (defun min-elem (ll) (min-elem1 ll (first ll)) ) (defun min-elem1 (ll tmin) (cond ((null ll) tmin) ((<= tmin (first ll)) (min-elem1 (rest ll) tmin)) (t (min-elem1 (rest ll) (first ll))) ) ) ;; quicksort (defun quicksort (ll) (cond ((< (length ll) 2) ll) (t (append (quicksort (small-elements (rest ll) (first ll))) (cons (first ll) (quicksort (large-elements (rest ll) (first ll)))) ) ) ) ) (defun small-elements (ll x) (cond ((null ll) nil) ((< (first ll) x) (cons (first ll) (small-elements (rest ll) x))) (t (small-elements (rest ll) x)) ) ) (defun large-elements (ll x) (cond ((null ll) nil) ((>= (first ll) x) (cons (first ll) (large-elements (rest ll) x))) (t (large-elements (rest ll) x)) ) ) ;; mergesort (defun mergesort (ll) (cond ((< (length ll) 2) ll) (t (merge-lists (mergesort (first-half ll)) (mergesort (second-half ll)))) ) ) (defun first-half (ll) (first-n-elements (/ (length ll) 2) ll) ) (defun second-half (ll) (not-first-n-elements (/ (length ll) 2) ll) ) (defun first-n-elements (n ll) (cond ((<= n 0) nil) (t (cons (first ll) (first-n-elements (- n 1) (rest ll)))) ) ) (defun not-first-n-elements (n ll) (cond ((<= n 0) ll) (t (not-first-n-elements (- n 1) (rest ll))) ) ) (defun merge-lists (l1 l2) (cond ((null l1) l2) ((null l2) l1) ((< (first l1) (first l2)) (cons (first l1) (merge-lists (rest l1) l2))) (t (cons (first l2) (merge-lists l1 (rest l2)))) ) )