Created
October 3, 2012 02:04
-
-
Save mtimkovich/3824517 to your computer and use it in GitHub Desktop.
llmergesort, midpoint, and dmerj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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