Sorting algorithms/Bubble sort: Difference between revisions

J style solution
(→‎{{header|Clojure}}: Clojure bubble sort, the most un-functional Clojure code I've written...)
(J style solution)
Line 569:
 
=={{header|J}}==
<lang J>bubbleSort=: verb define^:_
bubbleSort=: (([ (>. , <.) {.@]) , }.@])/^:_
for_n. i. (#y)-1 do.
pair=. y{~0 1+n
if. >!.0/pair do.
y=. pair (1 0+n)}y
end.
end.
y
)</lang>
 
Test program:
 
<lang J>
?. 10 $ 10
4 6 8 6 5 8 6 6 6 9
bubbleSort ?. 10 $ 10
9 8 8 6 6 6 6 6 5 4
</lang>
 
For the most part, bubble sort works against J's strengths. However, once a single pass has been implemented as a list operation, ^:_ tells J to repeat this until the result stops changing.
Anonymous user