Sorting algorithms/Bubble sort: Difference between revisions
Content added Content deleted
m (→{{header|J}}: fix lang tags) |
m (Fixed lang tags.) |
||
Line 344: | Line 344: | ||
Bubble sorts a Java ArrayList in place. Uses 'doseq' iteration construct with a short-circuit when a pass didn't produce any change, and within the pass, an atomic 'changed' variable that gets reset whenever a change occurs. |
Bubble sorts a Java ArrayList in place. Uses 'doseq' iteration construct with a short-circuit when a pass didn't produce any change, and within the pass, an atomic 'changed' variable that gets reset whenever a change occurs. |
||
⚫ | |||
<lang clojure> |
|||
⚫ | |||
(defn bubble-sort |
(defn bubble-sort |
||
Line 367: | Line 366: | ||
(doseq [stop-i (range (dec (.size a)) -1 -1) |
(doseq [stop-i (range (dec (.size a)) -1 -1) |
||
:while (sorter stop-i)]) |
:while (sorter stop-i)]) |
||
arr))) |
arr)))</lang> |
||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
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 |
||
Line 569: | Line 567: | ||
=={{header|J}}== |
=={{header|J}}== |
||
⚫ | |||
<lang j> |
|||
⚫ | |||
</lang> |
|||
Test program: |
Test program: |
||
Line 578: | Line 574: | ||
4 6 8 6 5 8 6 6 6 9 |
4 6 8 6 5 8 6 6 6 9 |
||
bubbleSort ?. 10 $ 10 |
bubbleSort ?. 10 $ 10 |
||
4 5 6 6 6 6 6 8 8 9 |
4 5 6 6 6 6 6 8 8 9</lang> |
||
</lang> |
|||
For the most part, bubble sort works against J's strengths. However, once a single pass has been implemented as a list operation, <code>^:_</code> tells J to repeat this until the result stops changing. |
For the most part, bubble sort works against J's strengths. However, once a single pass has been implemented as a list operation, <code>^:_</code> tells J to repeat this until the result stops changing. |