Sorting algorithms/Bubble sort: Difference between revisions

→‎Common Lisp: whitespace (parens; don't assume tabs=8); "from 0" unnecessary; use 1+, 1-; while not -> until
No edit summary
(→‎Common Lisp: whitespace (parens; don't assume tabs=8); "from 0" unnecessary; use 1+, 1-; while not -> until)
Line 345:
Bubble sort an sequence in-place, using the < operator for comparison if no comaprison function is provided
 
<lang lisp>(defun bubble-sort( (sequence &optional( (compare #'<))
"sort a sequence (array or list) with an optional comparison function (cl:< is the default)"
(loop with sorted = nil while (notuntil sorted) do
(setf sorted t)
(loop for a from 0 below (1- (length sequence) 1) do
(unless (funcall compare (elt sequence a) (elt sequence (+ a 1)))
(rotatef (elt sequence a) (elt sequence (1+ a 1)))
(rotatef (elt sequence a)
(setf sorted nil)))))
(elt sequence (1+ a)))
(setf sorted nil)))))</lang>
 
<lang lisp>(bubble-sort (list 5 4 3 2 1))</lang>
; main
 
(bubble-sort (list 5 4 3 2 1))
</lang>
 
=={{header|D}}==