;; | --------------------------------------------------------------------------- ;; | MI_mfix ;; | --------------------------------------------------------------------------- ;; | Function : Modified fix function, handles negative numbers also. ;; | Argument : 'val - Value to be fix'ed ;; | Return : The fix'ed value (always an integer) ;; | Update : March 24, 1998 ;; | e-mail : rakesh.rao@4d-technologies.com ;; | Web : www.4d-technologies.com ;; | --------------------------------------------------------------------------- (defun MI_mfix(val) (if (minusp val) (1- (fix val)) (fix val) ) ) ; Shell Sort (defun MI_ssort (lst / number_items partition_size number_partitions first_index last_index unsorted count ptr_lst sorted_list i j t1 t2) (if lst (progn (setq number_items (length lst) partition_size number_items ptr_lst nil ; Pointer list count 0 unsorted T ; Assume list is not sorted ) (while (< count number_items) (setq ptr_lst (append ptr_lst (list count)) ; built pointer list count (1+ count) ) ) (while unsorted (setq partition_size (fix (/ (1+ partition_size) 2)) number_partitions (fix (/ number_items partition_size)) ) (if (= partition_size 1) (setq unsorted nil) ; assume list is sorted ) (if (/= (rem number_items partition_size) 0) (setq number_partitions (1+ number_partitions)) ) (setq first_index 0 i 1 ) (while (< i number_partitions) (setq last_index (+ first_index partition_size)) (if (> last_index (- number_items partition_size)) (setq last_index (- number_items partition_size)) ) ; Loop thru and test (j) to (j+offset) in pointer list (setq j first_index) (while (< j last_index) (if (> (nth (nth j ptr_lst) lst) (nth (nth (+ j partition_size) ptr_lst) lst) ) ; Then swap items in pointer list (setq t1 (nth j ptr_lst) t2 (nth (+ j partition_size) ptr_lst) ptr_lst (subst t2 -1 (subst t1 t2 (subst -1 t1 ptr_lst) ) ) unsorted T )) (setq j (1+ j)) ) (setq first_index (+ first_index partition_size) i (1+ i) ) ) ) ; Build new list using sorted pointers (setq count 0 sorted_list nil) (while (< count number_items) (setq sorted_list (append sorted_list ; Build updated list (list (nth (nth count ptr_lst) ; Pointer lst ) ) ) count (1+ count) ) ) sorted_list ) nil ) )