Sorting algorithms/Permutation sort: Difference between revisions

Content added Content deleted
(markup messup)
Line 53: Line 53:
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==


Too bad <tt>sorted?</tt> vector code has to be copypasta'd. Could use <tt>map nil</tt> but that would in turn make it into spaghetti code.
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 <tt>nth-permutation</tt> function is some classic algorithm from Wikipedia.
The <code>nth-permutation</code> function is some classic algorithm from Wikipedia.


<pre>(defun factorial (n)
<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)))</pre>
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)))