Skip to content

Instantly share code, notes, and snippets.

@mtimkovich
Created October 3, 2012 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtimkovich/3824517 to your computer and use it in GitHub Desktop.
Save mtimkovich/3824517 to your computer and use it in GitHub Desktop.
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