(defun mergesort (li &optional (compare #'>)) (cond ((or (endp li) (endp (rest li))) li) (t (labels ((my-split (lis) (cond ((endp lis) (list nil nil)) ((endp (rest lis)) (list lis nil)) (t (let* ((fi (first lis)) (la (first (reverse (rest lis)))) (mi (reverse (rest (reverse (rest lis))))) (re (my-split mi))) (list (append (list fi) (first re)) (append (second re) (list la))))))) (my-merge (lis) (let ((fi (first lis)) (se (second lis))) (cond ((endp fi) se) ((endp se) fi) (t (cond ((funcall compare (first se) (first fi)) (cons (first se) (my-merge (list fi (rest se))))) (t (cons (first fi) (my-merge (list (rest fi) se)))))))))) (let* ((sp (my-split li)) (le (mergesort (first sp) compare)) (ri (mergesort (second sp) compare))) (my-merge (list le ri)))))))