Sorting algorithms/Bubble sort: Difference between revisions

m
Fixed lang tags.
m (→‎{{header|J}}: fix lang tags)
m (Fixed lang tags.)
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.
<lang lisp>(import 'java.util.ArrayList)
<lang clojure>
(import 'java.util.ArrayList)
 
(defn bubble-sort
Line 367 ⟶ 366:
(doseq [stop-i (range (dec (.size a)) -1 -1)
:while (sorter stop-i)])
arr)))</lang>
</lang>
=={{header|Common Lisp}}==
Bubble sort an sequence in-place, using the < operator for comparison if no comaprison function is provided
Line 569 ⟶ 567:
 
=={{header|J}}==
<lang j>bubbleSort=: |. @: ((([ (>. , <.) {.@]) , }.@])/^:_)</lang>
<lang j>
bubbleSort=: |. @: ((([ (>. , <.) {.@]) , }.@])/^:_)
</lang>
 
Test program:
Line 578 ⟶ 574:
4 6 8 6 5 8 6 6 6 9
bubbleSort ?. 10 $ 10
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.
Anonymous user