Created
July 5, 2013 17:51
-
-
Save ser1zw/5936129 to your computer and use it in GitHub Desktop.
Common Lispでクイックソートの練習
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; -*- mode: lisp; coding: utf-8 -*- | |
(defun quick-sort(lst &optional (test-func #'<)) | |
(if (<= (length lst) 1) | |
lst | |
(let (upper | |
lower | |
(pivot (car lst))) | |
(dolist (v (cdr lst)) | |
(if (funcall test-func v pivot) | |
(push v upper) | |
(push v lower))) | |
(append (quick-sort upper test-func) | |
(list pivot) | |
(quick-sort lower test-func))))) | |
(setf data '(3 1 4 1 5 9 2 6 5 3 5 8 9 7 9)) | |
(format t "Data: ~S~%" data) | |
(format t "Asc: ~S~%" (quick-sort data)) | |
(format t "Desc: ~S~%" (quick-sort data #'>)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment