Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

insertion sort

  • Loading branch information...
commit 9e242569358496cf4a5a6ee40a161f238b57941e 1 parent e1bd700
@unionx authored
Showing with 26 additions and 0 deletions.
  1. +26 −0 clrs/insertion-sort.lisp
View
26 clrs/insertion-sort.lisp
@@ -0,0 +1,26 @@
+(defun gen-unsorted-lst ()
+ (let ((x '()))
+ (loop
+ for i from 0 to 99
+ do (setf x (append x (list (random 1000)))))
+ x))
+
+(defun insertion-sort (lst)
+ (let ((x '()))
+ (loop
+ for i in lst
+ do (setf x (insert-into-lst i x)))
+ x))
+
+(defun insert-into-lst (elt lst)
+ (if (equal nil lst)
+ (list elt)
+ (let ((tail-idx (1- (length lst))))
+ (loop
+ for i from 0 to tail-idx
+ if (> (nth i lst) elt)
+ do (return-from insert-into-lst (append (subseq lst 0 i) (list elt) (subseq lst i)))
+ if (= i tail-idx)
+ do (return-from insert-into-lst (append lst (list elt)))))))
+
+(insertion-sort (gen-unsorted-lst))
Please sign in to comment.
Something went wrong with that request. Please try again.