snippetMinor
Insertion sort in Common Lisp
Viewed 0 times
insertioncommonsortlisp
Problem
This is my first bit of significant code in Common Lisp, an implementation of insertion sort. Being new to Lisp in general, I'm wondering if this code follows best Lisp practices in regards to program design.
;;; Return a portion of a list
(defun slice (elements from-index to-index)
(cond
((= from-index to-index) nil)
(t (cons (nth from-index elements) (slice elements (+ from-index 1) to-index)))))
;;; Return a list omitting a portion
(defun splice (elements from-index to-index)
(append (slice elements 0 from-index) (slice elements to-index (length elements))))
;;; Move element in list to new index, return new list
(defun move (elements from-index to-index)
(let ((spliced (splice elements from-index (+ from-index 1))))
(append (slice spliced 0 to-index) (list (nth from-index elements)) (slice spliced to-index (length spliced)))))
;;; Find the determined (via predicate) correct index for an element in a list
(defun find-ordered-index (elements predicate index &optional (key index))
(cond
((or (= index 0) (= key 0)) 0)
((funcall predicate (nth (- key 1) elements) (nth index elements)) (find-ordered-index elements predicate index (- key 1)))
(t key)))
;;; Return sorted list
(defun insertion-sort (elements predicate &optional (index 0))
(cond
((= index (length elements)) elements)
(t (insertion-sort (move elements index (find-ordered-index elements predicate index)) predicate (+ index 1)))))Solution
Trivial
Use
Avoid very long lines (Emacs will indent for you).
Do not use
Memory
Use
unnecessary consing (in your case,
its result can be passed to
Catastrophic
Whenever you use
using the wrong algorithm.
Optimal search is linearithmic:
Insert search is quadratic:
Your implementation is
Use
1- instead of (- ... 1).Avoid very long lines (Emacs will indent for you).
Do not use
cond when a single if without progn would do.Memory
Use
nconc instead ofappend when possible to avoidunnecessary consing (in your case,
splice allocates a fresh list, soits result can be passed to
nconc).Catastrophic
Whenever you use
nth, you areusing the wrong algorithm.
Optimal search is linearithmic:
O(n*log(n)).Insert search is quadratic:
O(n^2).Your implementation is
O(n^3):nthisO(n)
sliceisO(n^2)(callsnth* recursion).
spliceandmoveare alsoO(n^2)(callslice).
find-ordered-indexisO(n).
insertion-sortisO(n^3)(callsmove* recursion).
Context
StackExchange Code Review Q#97725, answer score: 6
Revisions (0)
No revisions yet.