; SORTS.LSP
; A comparison of two of the 'classic' sort routines - the Bubble and Shell
: from http://ourworld.compuserve.com/homepages/pshan/acptrick.htm
;
; In each case the sort is performed on the 2nd member of a three member list
; in the following format ("string" REAL REAL)
;
; it Returns the sorted list.
(defun Shell ( ALIST / BEGSORT CTR EXCH ELAPSE ENDSORT HEAD N NEWLIST TAIL)
;;
;; The Shell Sort (ascending)
;;
(defun EXCH ( / EX_FLAG HEAD MID NEW1ST NEW2ND TAIL)
(setq EX_FLAG 1)
(while EX_FLAG
(setq
NEW1st (list (nth (+ CTR D ) NEWLIST)) ; Switch value at D interval to CURRENT
NEW2nd (list (nth CTR NEWLIST)) ; Switch CURRENT value to D interval
HEAD (reverse (cdr (member (nth CTR NEWLIST) (reverse NEWLIST)))) ; First part of list
MID (member (nth (1+ CTR) NEWLIST) (reverse
(member (nth (1- (+ CTR D)) NEWLIST) (reverse NEWLIST)))) ; Between CURRENT and D interval
TAIL (cdr (member (nth (+ CTR D) NEWLIST) NEWLIST)) ; Remainder of list
NEWLIST (append HEAD NEW1st MID NEW2nd TAIL) ; Revised list
CTR (- CTR D)
)
(if (>= CTR 0)
(progn
(if (<= (cadr (nth CTR NEWLIST)) (cadr (nth (+ CTR D) NEWLIST))) ; Add Jump Dist to Current location
(setq EX_FLAG nil))
);progn
(setq EX_FLAG nil)
)
);WHILE
)
(if (or (null ALIST)
(not (= (type ALIST) 'LIST ))
)
(progn
(princ "Argument is not a valid list")(print)
)
(progn
(setq
NEWLIST ALIST ; Make a copy to work on
D (abs (/ (length NEWLIST) 2)) ; D will be an integer
N (1- (length NEWLIST)) ; first member of list is 0