Skip to content

Instantly share code, notes, and snippets.

@lispm
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lispm/5990c341a20003de493b to your computer and use it in GitHub Desktop.
Save lispm/5990c341a20003de493b to your computer and use it in GitHub Desktop.
non-recursive quicksort
;;; non-recursive quicksort
;;; http://bertrandmeyer.com/2014/12/07/lampsort/
(defun partition (array low high)
(let ((pivot-value (aref array high))
(insert-at low))
(loop for i from low upto high do
(when (< (aref array i) pivot-value)
(rotatef (aref array i) (aref array insert-at))
(incf insert-at)))
(rotatef (aref array high) (aref array insert-at))
insert-at))
(defun quicksort-lamport (array predicate &aux (intervals (list (cons 0 (1- (length array))))))
(loop while intervals do
(destructuring-bind (low . high)
(pop intervals)
(when (funcall predicate low high)
(let ((pivot (partition array low high)))
(push (cons low (1- pivot)) intervals)
(push (cons (1+ pivot) high) intervals)))))
array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment