18 Mar 2002 21:15
Re:sort
I can't really think of a good reason for doing this, but if you resort to
surgery, you can sort the list that's passed in as an arg. It makes an
interesting contrast to the behaviour of SORT, but not an algorithm you
would willingly use ...
(defun surgical-sort (in-list)
(let ((length (1- (length in-list)))
temp)
(dotimes (i length)
(let ((current in-list)
(next (cdr in-list)))
(dotimes (j (- length i))
(when (> (car current) (car next))
;; perform surgery
(setf temp (car next))
(setf (car next) (car current))
(setf (car current) temp))
(setf current next)
(setf next (cdr current))))))
in-list)
;;; (setf *list* '(2 9 34 1 0 54 79 3 7 8))
(2 9 34 1 0 54 79 3 7 8)
;;; (surgical-sort *list*)
(0 1 2 3 7 8 9 34 54 79)
;;; *list*
(0 1 2 3 7 8 9 34 54 79)
-Glen
RSS Feed