Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
llmergesort, midpoint, and dmerj
(defun midpoint (lst)
(setq current lst)
(setq prev lst)
(loop while (and lst (rest lst)) do
(setq lst (rest (rest lst)))
(setq prev current)
(setq current (rest current)))
prev)
(defun dmerj (x y)
(cond
((not x) y)
((not y) x)
(t
(if (< (first x) (first y))
(progn
(setq front x)
(setq x (rest x)))
(progn
(setq front y)
(setq y (rest y))))
(setq end front)
(loop while x do
(if (or (not y) (< (first x) (first y)))
(progn
(setf (rest end) x)
(setq x (rest x)))
(progn
(setf (rest end) y)
(setq y (rest y))))
(setq end (rest end)))
(setf (rest end) y)
front)))
(defun llmergesort (lst)
(let (mid half)
(if (or (not lst) (not (rest lst)))
lst
(progn
(setq mid (midpoint lst))
(setq half (rest mid))
(setf (rest mid) nil)
(dmerj (llmergesort lst)
(llmergesort half))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment