;; The insertion sort method which sorts array of numbers only (defun insertion-sort (arr) (loop for i from 1 to (1- (length arr)) do (let( (key (aref arr i)) (j (1- i))) (loop while (and (> j -1) (< key (aref arr j))) do (setf (aref arr (1+ j)) (aref arr j)) (setq j (1- j))) (setf (aref arr (1+ j)) key) )) arr) ;; Generic insertion sort, which sorts arrays of any type using funcall. ;; lt and gt are lessthan and greaterthan functional parameters ;; call this function with (generic-insertion-sort #( "Ellie" "Mark" "Mariam" "Ali") #'string< ), in the debug window (defun generic-insertion-sort (arr lt) (loop for i from 1 to (1- (length arr)) do (let( (key (aref arr i)) (j (1- i))) (loop while (and (> j -1) (funcall lt key (aref arr j))) do (setf (aref arr (1+ j)) (aref arr j)) (setq j (1- j))) (setf (aref arr (1+ j)) key) )) arr)