Sorting algorithms/Permutation sort: Difference between revisions
Content added Content deleted
(markup messup) |
(→{{header|Common Lisp}}: markup) |
||
Line 53: | Line 53: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Too bad < |
Too bad <code>sorted?</code> vector code has to be copypasta'd. Could use <tt>map nil</tt> but that would in turn make it into spaghetti code. |
||
The < |
The <code>nth-permutation</code> function is some classic algorithm from Wikipedia. |
||
< |
<lang lisp>(defun factorial (n) |
||
(loop for result = 1 then (* i result) |
(loop for result = 1 then (* i result) |
||
for i from 2 to n |
for i from 2 to n |
||
Line 90: | Line 90: | ||
for permutation = (nth-permutation i sequence) |
for permutation = (nth-permutation i sequence) |
||
when (sorted? fn permutation) |
when (sorted? fn permutation) |
||
do (return permutation)))</ |
do (return permutation)))</lang> |
||
<pre>CL-USER> (time (permutation-sort #'> '(8 3 10 6 1 9 7 2 5 4))) |
<pre>CL-USER> (time (permutation-sort #'> '(8 3 10 6 1 9 7 2 5 4))) |