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