Sorting algorithms/Selection sort: Difference between revisions

Content added Content deleted
(add E example)
(selection sort in Common Lisp for arrays)
Line 190: Line 190:
200
200
</pre>
</pre>

=={{header|Common Lisp}}==

<lang lisp>(defun selection-sort (array predicate)
"Sort array according to predicate."
(do ((length (length array))
(i 0 (1+ i)))
((eql i length) array)
(do ((mindex i)
(min (aref array i))
(j i (1+ j)))
((eql j length)
(rotatef (aref array i) (aref array mindex)))
(when (funcall predicate (aref array j) min)
(setf min (aref array j)
mindex j)))))</lang>

Example use:

<pre>> (defparameter *vec* (vector 1 0 2 9 3 8 4 7 5 6))
*VEC*

> (selection-sort *vec* '<)
#(0 1 2 3 4 5 6 7 8 9)

> (selection-sort *vec* '>)
#(9 8 7 6 5 4 3 2 1 0)</pre>


=={{header|D}}==
=={{header|D}}==